Jump to content
Larry Ullman's Book Forums

foreign key constraint, which table first

Recommended Posts

Which table do I insert to first, table1 that does have the FOREIGN KEY (file_id) REFERENCES userinfo(user_id) or the table2 that does NOT have the constrain?
 I am trying to insert into both tables at a sequence, table1 and then table2, BUT I get this error: "Cannot add or update a child row: a foreign key constraint fails...!"
Should I insert first into table2 and then into table1? Thanks

 $q1 = "INSERT INTO table1 ...
 $r1 = @mysqli_query($dbc, $q1); // Run the query.

 if ($r1) { // If it ran OK.
            $q2 = "INSERT INTO table2 ...
            $r2 = @mysqli_query($dbc, $q2);

Link to comment
Share on other sites


  • Create New...