【发布时间】:2014-03-07 16:48:27
【问题描述】:
我正在尝试创建一个语句,以便在按下提交按钮时将数据传递到两个表(属性、前)中。
PK 会自动添加到属性表中。然后需要将此 PK 与提交的其余数据一起作为 FK 拉出并添加到前表中。
当我修改语句以仅将数据插入属性表时,它可以正常工作。但是,当输入第二个表的信息时,一切都停止工作。我知道该声明一定有问题,但我不知道是什么。我将不胜感激任何建议。 谢谢你 提前。
PropertyID 是属性的 PK 和前端的 FK。它是一个自动增量,这就是为什么它没有包含在代码的第一部分中。
我正在尝试创建的声明:
$sql = "START TRANSACTION ;
INSERT INTO properties ( PropertyType, AH, Bedrooms, Bathrooms, Ensuite, Kitchen, LivingRoom, DiningRoom, UtilityRoom, Conservatory, Garage, Garden, Parking,Furnished, Description, PostCode, AddL1, AddL2, AddL3,Area,County,Country)
VALUES( '$PropertyType', '$AH', '$Bedrooms','$Bathrooms', '$Ensuite', '$Kitchen', '$LivingRoom', '$DiningRoom', '$UtilityRoom', '$Conservatory', '$Garage','$Garden', '$Parking','$Furnished', '$Description', '$PostCode','$AddL1', '$AddL2', '$AddL3','$Area','$County','$Country');
INSERT INTO frent (FRent,LAST_INSERT_ID(PropertyID), MinCon, PaymentExp, RCost)
VALUES ('FRent','$PropertyID', '$MinCon', '$PaymentExp','$RCost');
COMMIT";
【问题讨论】:
-
检查这个线程,stackoverflow.com/questions/3837990/last-insert-id-mysql 其他选项是先执行插入查询,然后使用 mysql_insert_id() 获取最后插入的 id