我正在使用三表結構來處理多對多關係。我有一張有人名單的表格,另一張表格有一個項目清單。有時候多的人有相同的項目,有時倍數項目鏈接到同一個人,所以我設置如下表結構:在項目已存在時更新中間表時出現問題
CREATE TABLE people (
id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
fname varchar(128) NOT NULL,
lname varchar(128) NOT NULL,
);
CREATE TABLE items (
id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(128) NOT NULL UNIQUE,
);
的UNIQUE防止項目名稱重複從。
CREATE TABLE people_items (
pid int(11) NOT NULL,
iid int(11) NOT NULL,
FOREIGN KEY (pid) REFERENCES people(id)
ON UPDATE CASCADE
ON DELETE CASCADE
FOREIGN KEY (iid) REFERENCES items(id)
ON UPDATE CASCADE
ON DELETE CASCADE
);
這使我可以將多個項目鏈接到多個人,反之亦然。它也允許我從中間表中刪除不必要的記錄。
只要輸入一個新項目,所有工作都會正常工作,但是如果輸入了一個現有項目,即使人員表是,中間表也不會更新。我也沒有得到任何錯誤。
項目是一個逗號分隔的文本條目,它們被分解並放入$items
中。
首先我插入任何新項目和檢索ID:
for ($i=0;$i<count($items);$i++){
$sql="INSERT IGNORE INTO items (name) VALUES (?);";
$stmt=$conn->prepare($sql);
$stmt->bind_param('s',$items[$i]);
$stmt->execute();
$itemid=$stmt->insert_id;
如果返回一個新的ID執行以下操作:
if ($itemid){
$sql="INSERT INTO people_items (pid,iid) VALUES (?,?);";
$stmt=$conn->prepare($sql);
$stmt->bind_param('ii',$peopleid,$itemid);//the $peopleid is acquired the same way that the $itemid is acquired above
$stmt->execute();
}
到這裏,一切都運行得很好。此時,如果現有的項目已經在項目表中,那麼我的中間表不會更新。然而,人員表更新得很好,而且物品表不需要更新,因爲它已經包含了物品。
這裏是我嘗試了兩種不同的方法來更新中間表。
首先我保持選擇和插入查詢分開。
elseif(!$itemid){
$sql="SELECT id,name FROM items WHERE name=?;";
$stmt=$conn->prepare($sql);
$stmt->bind_param('s',$items[$i]);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($iid,$name);
$stmt->fetch();
$sql="INSERT INTO people_items (pid,iid) VALUES (?,?);";
$stmt=$conn->prepare($sql);
$stmt->bind_param('ii',$pid,$iid);
$stmt->execute();
}
這裏是我也不會更新中間表的另一種方法:
elseif(!$itemid){
$sql="INSERT INTO people_items (pid, iid) SELECT id,name FROM items WHERE name IN (?);";
$stmt=$conn->prepare($sql);
$stmt->bind_param('s',$items[$i]);
$stmt->execute();
}
我在做什麼錯?