简体   繁体   中英

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 . The file is here: 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:

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?

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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