MySQL Forums
Forum List  »  PHP

How to wait for INSERT to happen before moving onto next INSERT query?
Posted by: Mike Liedike
Date: March 15, 2013 02:09PM

I am inserting data into, table1. After this data is inserted I am trying to insert data into table2 to based off table1 data. Table2 has FK from table one that I am referencing. This is how I have my inserts setup up right now (this does not work). Any help on how to clean this up and make it work correctly would be greatly appreciated.

$r = mysqli_query($dbc, "INSERT INTO player(gamedate, home, away, goalsfor, goalsagainst, yellowcards, redcards) VALUES ('$gamedate','$home','$away','$goalsfor','$goalsagainst','$yellowcards','$redcards')");
if (mysqli_affected_rows($dbc) == 1) {
	echo "Added game successfully";
	$playerid = mysql_insert_id();
	$gameresult = mysqli_query($dbc, "INSERT INTO game(player_id, pposition, pgoalsfor, pgoalsagainst, psog, ppk, pyellowcards, predcards) VALUES ('$playerid','$pposition','$pgoalsfor','$pgoalsagainst','psog','ppk','pyellowcards','predcards')");
	if (mysqli_affected_rows($dbc) == 1) {
		echo "Added Individual Stats successfully";
		mysqli_close();
	}
	else {
		echo "There was an error.  Your stats were not added.";	
		mysqli_close();
	}
	mysqli_close();
}

else {
	echo "There was an error.  Your game was not added.";	
	mysqli_close();
}

Thank you,
Mike

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.