【问题标题】:How to Insert Data into two tables linked by a foreign key?如何将数据插入由外键链接的两个表中?
【发布时间】: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";

【问题讨论】:

标签: php sql forms


【解决方案1】:

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

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

$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";

【讨论】:

    猜你喜欢
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2011-04-29
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多