MySQL Forums
Forum List  »  PHP

Re: How to wait for INSERT to happen before moving onto next INSERT query?
Posted by: Barry Galbraith
Date: March 15, 2013 09:45PM

> I tried using mysqli_error but I did not get any errors returned.

How?

I might do something like this
$sql = "INSERT INTO player(gamedate
			, home
			, away
			, goalsfor
			, goalsagainst
			, yellowcards
			, redcards
			) VALUES (
			'$gamedate'
			,'$home'
			,'$away'
			,'$goalsfor'
			,'$goalsagainst'
			,'$yellowcards'
			,'$redcards'
			)";
// echo the $sql to see what you've got. remove it when finished debugging
echo $sql;
$r = mysqli_query($dbc, $sql ) or die(mysqli_error($dbc));

if (mysqli_affected_rows($dbc) == 1) {
	echo "Added game successfully";
	$playerid = mysqli_insert_id();
	$sql = "INSERT INTO game(
		player_id
		, pposition
		, pgoalsfor
		, pgoalsagainst
		, psog
		, ppk
		, pyellowcards
		, predcards
                )
		VALUES (
		'$playerid'
		,'$pposition'
		,'$pgoalsfor'
		,'$pgoalsagainst'
		,'$psog'
		,'$ppk'
		,'$pyellowcards'
		,'$predcards'
		)"
	echo $sql;
	$gameresult = mysqli_query($dbc, $sql) or die(mysqli_error($dbc));;
	if (mysqli_affected_rows($dbc) == 1) {
		echo "Added Individual Stats successfully";
	}
	else {
		echo "There was an error.  Your stats were not added.";	
	}
	mysqli_close();
}

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

You were missing the $ before the last 4 variables in the second insert. Polling for errors will show you that.
Of course, all this hangs on $dbc being a valid connection to the database.
You don't show us that, and it will need to be tested by polling mysqli_error() after the connection is made.

Good luck,
Barry.

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.