Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 包含数据库完整性_Php_Mysql - Fatal编程技术网

Php 包含数据库完整性

Php 包含数据库完整性,php,mysql,Php,Mysql,好的,这是一个直截了当的问题,但我有一个问题,我应该如何着手实施解决方案 因此,这里是数据库结构的外观,它大大减少了所需的一切 表包括事件、联系人、联系人、事件角色、事件角色 create table events( event_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(200) NOT NULL, PRIMARY KEY(event_id) ); INSERT INTO events VALUES(1, '

好的,这是一个直截了当的问题,但我有一个问题,我应该如何着手实施解决方案

因此,这里是数据库结构的外观,它大大减少了所需的一切

表包括事件、联系人、联系人、事件角色、事件角色

create table events(
  event_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(200) NOT NULL,
  PRIMARY KEY(event_id)
);

INSERT INTO events VALUES(1, 'stackoverflow');
INSERT INTO events VALUES(2, 'throwsanerror');

create table contacts(
  contact_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  fname VARCHAR(40) NOT NULL,
  lname VARCHAR(40) NOT NULL,
  email VARCHAR(90) NOT NULL,
  PRIMARY KEY(contact_id)
);

INSERT INTO contacts VALUES(1, 'bill', 'smith', 'bsmith@email.com');
INSERT INTO contacts VALUES(2, 'amy', 'lee', 'amylee@email.com');

event_roles(
  role_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  description VARCHAR(80),
  PRIMARY KEY(role_id)
);

//The roles look something like this
INSERT INTO event_roles VALUES(1, 'Event Coordinator');
INSERT INTO event_roles VALUES(2, 'Decision Maker');
INSERT INTO event_roles VALUES(3, 'Inquiry Contact');

contacts_event_role(
  event_id INTEGER UNSIGNED NOT NULL,
  contact_id INTEGER UNSIGNED NOT NULL,
  role_id INTEGER UNSIGNED NOT NULL,
  FOREIGN KEY(event_id) REFERENCES events(event_id),
  FOREIGN KEY(contact_id) REFERENCES contacts(contact_id),
  FOREIGN KEY(role_id) REFERENCES event_roles(role_id),
  PRIMARY KEY(event_id, role_id)
);

INSERT INTO event_role VALUES(1, 1, 1);
INSERT INTO event_role VALUES(1, 1, 2);
INSERT INTO event_role VALUES(2, 2, 1);
这就是数据库的要点。 用一点伪数据。我很确定那里一切都很好

这就是我的逻辑

我试图做的是插入/更新客户机和角色,必要时让客户机填充多个角色

我的伪代码如下所示

//perform a check to see if the event_role is being filled...
check4role = SELECT * FROM contacts_event_role WHERE role_id = 1 AND event_id = 1

//perform a check to see if the contact already exists.
check4contact = SELECT * FROM contacts WHERE fname = :fname AND lname = :lname AND email = :email; 

//if the role is already being filled && contact exists 

if( check4role == true && check4contact == true)
  UPDATE contact_event_role

//else if the role exists and contact does not exists

elseif( check4role == true && check4contact == false)
  INSERT INTO contacts 
  UPDATE contact_event_role

//else if the role does not exists and the contact does exist

elseif( check4role == false && check4contact == true)
  INSERT INTO contact_event_role

//else if the role does not exists and the contact does not exist

elseif( check4role == false && check4contact == false)
  INSERT INTO contacts
  INSERT INTO contact_event_role
你知道我不确定的是什么,但我想我只是说出了正确的逻辑,但我真的很想得到一些反馈,看看这是怎么做的,还是我的逻辑有缺陷。我觉得我错过了什么


谢谢

我觉得不错,但是你可以让你的代码更容易阅读 将其更改为如下所示:

if (check4contact == false)
{
    insert into contacts
}

if (check4role == false)
{
    INSERT INTO contact_event_role
}
else
{
    UPDATE contact_event_role
}
我把它分成了两张单独的支票来解决这个问题。我没有试图处理每一种可能的情况,而是将代码分成了“你想做什么”的部分。第一个是确保联系人存在。如果没有,我们就创造它们

然后,第二部分是更新联系人\事件\角色表

您也可以通过移出公共代码来实现这一点——例如,您两次调用“INSERT INTO contacts”,两次都是因为“check4contact”为false而调用的。所以这是清理你的逻辑的一种方法