[英]What do I have to insert in sql foreign key column between two tables
我創建了兩個表, whole_data
和room_table
。 我拿着的ID whole_data
作為一個外鍵room_table
,我通過HTML表單中插入數據,而不是手動。 所以現在我很困惑,我需要在room table
的外鍵字段中插入什么。 查看代碼和查詢。 如果你看一下room_table
中的插入,那么第四列的值我把它留空了,因為我不知道我要插入什么,我需要的只是whole_data
表的主要id。
$whole_tab = "CREATE TABLE IF NOT EXISTS whole_tab (
p_id int(100) NOT NULL AUTO_INCREMENT PRIMARY KEY,
agentName varchar(400),
price int(100),
grossArea varchar(100),
postCode varchar(50),
pricePerSqFt varchar(100),
prType varchar(100),
contact varchar(200),
prDesc varchar(5000),
prImgs varchar(3000),
prPdf varchar(1000),
prAddress varchar(1000)
)";
$con->query($whole_tab);//Connect to table
//Table one end
$room_tab = "CREATE TABLE IF NOT EXISTS room_tab (
r_id int(100) NOT NULL AUTO_INCREMENT PRIMARY KEY,
bedRooms int(100),
bathRooms int(100),
otherList varchar(3000),
from_p_id int(100) REFERENCES whole_tab(p_id)
)";
$con->query($room_tab);//Connect to table
//INSERTION
$ins_whole_tab = "INSERT INTO whole_tab VALUES(NULL,'$agName','$prPrice','$prGrossArea','$postCode','$prPerSqFt','$radioSel','$prContact','$prDesc','$imgPathsJson','$pdfPath','$prAddress')";
$con -> query($ins_whole_tab);
//Insert Data
$ins_room_tab = "INSERT INTO room_tab VALUES(NULL,'$bedRNum','$bathRNum','$otherRDet','')";
$con -> query($ins_room_tab);
last_insert_id()
返回插入的上一個自動增量ID的值。 在查詢中使用它:
$ins_room_tab = "INSERT INTO room_tab VALUES(NULL,'$bedRNum','$bathRNum','$otherRDet',last_insert_id())";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.