[英]MySQL INSERT using PHP fail: a foreign key constraint fails
My add.php
file is used to add submission of a form into 5 different tables namely: parents
, children
, child_state
, others
and contributions
. 我的add.php
文件用于将表单提交添加到5个不同的表中,即: parents
, children
, child_state
, others
人和contributions
。 The file is here: https://gist.github.com/anonymous/ca990a7de5458248fe3a 该文件在这里: https : //gist.github.com/anonymous/ca990a7de5458248fe3a
Inserting data into the parents
table works fines, but inserting to children
, child_state
and others
tables fails with the below error: 将数据插入到parents
表工作的罚款,但插入到children
, child_state
和others
表失败,下面的错误:
Cannot add or update a child row: a foreign key constraint fails 无法添加或更新子行:外键约束失败
Can a PHP/MySQL genius be able to point out my mistake or error? PHP / MySQL天才能否指出我的错误或错误?
mysqli_insert_id
only returns the id of the last inserted row if the table has an AUTO_INCREMENT field (if you don't have an AUTO_INCREMENT on one of your fields in your table, then the function mysql_insert_id
will return 0). 如果表具有AUTO_INCREMENT字段,则mysqli_insert_id
仅返回最后插入行的ID(如果表中的某个字段上没有AUTO_INCREMENT,则函数mysql_insert_id
将返回0)。 Your table doesn't seem to have one. 您的桌子似乎没有桌子。 Either changed f_id
to have an AUTO_INCREMENT attribute or get the ID of the last inserted row differently. 将f_id
更改为具有AUTO_INCREMENT属性,或者以不同的方式获取最后插入的行的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.