INSERT INTO TBL_payments未定义的索引以及父和子外键

时间:2015-12-04 11:10:09

标签: php mysql

所以......这是我的代码:

$paymentType=$_POST["paymentType"]; // in the first two lines the $_POST data is copied into normal variables to make it easier to handle
$paymentMethod=$_POST["paymentMethod"];
$amount=$_POST["amount"];
$date=$_POST["date"];
$other=$_POST["other"];

$mysqlserver="localhost"; // in the next six lines the connection to the server is set up just like before and the database is chosen
$mysqlusername="jakedean";
$mysqlpassword="jakedean";
$link=mysql_connect($mysqlserver, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

$dbname = 'jakedean';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
            // the query which inserts the new data (from the variables) is set up and run

$addPaymentquery="INSERT INTO TBL_payments
    (paymentType, paymentMethod, amount, date, other)
    VALUES 
    ('$paymentType', '$paymentMethod', '$amount', '$date', '$other')";
mysql_query($addPaymentquery) or die("Query to insert new Payment into TBL_payments failed with this error: ".mysql_error());            


echo "<p class=\"thicker2\">You added a new Payment to Person ID:$personId! The Payment information added was:</p><p class=\"bold\">Payment Type:</p> <p class=\"thicker\">$paymentType</p> 
        <p class=\"bold\">Payment Method:</p> <p class=\"thicker\">$paymentMethod</p><p class=\"bold\">Amount Paid:</p> <p class=\"thicker\">$amount</p><p class=\"bold\">Date of Payment:</p> <p class=\"thicker\">$date</p>
        <p class=\"bold\">Additional Comments:</p> <p class=\"thicker\">$other</p>";

错误是未定义索引的组合&#39;并且a无法更新子行,因为我已将sql表与错误相关...

错误消息:

Error message

我不知道这是否有帮助,但在我的数据库中,不同表格中的某些字段具有相同的名称......

我已经试图解决这个问题好几天但是我无法弄明白,这是我建立的第一个数据库!感谢任何回复!

2 个答案:

答案 0 :(得分:1)

未定义的索引是因为您尝试从数组中获取不存在的属性。 因为TBL_payments必须与TBL_people相关,因此您必须将TBL_paymentspersonId设置为当前的ID用户(登录的用户或此付款所属的用户) UPD:
当然,不要像评论中提到的那样使用已弃用的那些!

答案 1 :(得分:0)

在定义引用它的外键之前,您需要已经声明了外键引用的表。

声明第二个表后,可以声明第一个表。在MySQL 5.5.27上进行了测试。

如果您需要绕过验证,可以执行以下操作:

SET FOREIGN_KEY_CHECKS = 0; - 声明表格 SET FOREIGN_KEY_CHECKS = 1; http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_foreign_key_checks