如何将数据插入到由外键链接的两个表中?

时间:2014-03-07 16:48:28

标签: php sql forms

我正在尝试创建一个语句,以便在按下提交按钮时,数据将传递到两个表(属性,自由)。

PK会自动添加到属性表中。然后需要将此PK拉出并作为FK添加到frent表中,以及提交的其余数据。

当我修改语句以仅将数据插入属性表时,它可以正常工作。但是,当输入第二个表的信息时,一切都停止工作。我知道声明肯定有问题,但我不知道是什么。我会很感激任何建议。 谢谢 提前。

PropertyID是属性的PK和frent的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";

1 个答案:

答案 0 :(得分:0)

我认为表字段的措辞中的错误位于第二个表中。对我来说,看起来好像这是一个函数调用而不是正确的字段名称

将您的查询更改为以下内容:

$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, PropertyID, MinCon, PaymentExp, RCost)
    VALUES ('FRent',LAST_INSERT_ID(), '$MinCon', '$PaymentExp','$RCost');
    COMMIT";
相关问题