Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

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

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...