x_wd11 Posted August 5, 2019 Share Posted August 5, 2019 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); if($r2){... Link to comment Share on other sites More sharing options...
x_wd11 Posted August 7, 2019 Author Share Posted August 7, 2019 I found this link https://codeburst.io/insert-with-select-statement-for-columns-with-foreign-key-constraint-in-mysql-with-examples-f9ab57c8e4dd and fixing some of the errors in my query I got it working. Link to comment Share on other sites More sharing options...
Larry Posted August 13, 2019 Share Posted August 13, 2019 Excellent! Kudos for figuring it out and thanks for letting us know. Link to comment Share on other sites More sharing options...
Recommended Posts