简体   繁体   English

使用PHP的MySQL INSERT失败:外键约束失败

[英]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个不同的表中,即: parentschildrenchild_stateothers人和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表工作的罚款,但插入到childrenchild_stateothers表失败,下面的错误:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM