Jump to content

mysqli_insert_id - Doesn't Work


chaseman

Recommended Posts

I'm trying to implement an email activation method for my registration script. For that I need the last inserted id straight off the query.

 

This is how the part of my script looks like:

 

	// write into database
		$query = sprintf("INSERT INTO user (user_id, firstname, lastname, nickname, password, email, dob, doj, random, activated) VALUES (' ', '%s', '%s', '%s', '%s', '%s', '%s', now(), '$random', '0')",
					mysqli_real_escape_string($dbc, $firstname),
					mysqli_real_escape_string($dbc, $lastname),
					mysqli_real_escape_string($dbc, $nickname),
					mysqli_real_escape_string($dbc, $password),
					mysqli_real_escape_string($dbc, $user_email),
					$dob);

	echo $lastid = mysqli_insert_id($dbc) or die (mysqli_error($dbc));

 

The query itself WORKS, the data gets inserted correctly, the scripts dies right at the die of mysqli_insert_id.

 

When I take away the "or die", then I always get printed out a 0 when trying to register, THOUGH the data entered into the registration page gets inserted correctly into the database as said.

 

I've read on w3schools.com, that the connection ($dbc) is OPTIONAL in mysqli_insert_id($dbc), because it automatically takes the last active connection, but when I leave it empty I get an error saying that it needs at least one parameter.

 

I must be doing something wrong, if you need more of the script let me know.

 

Since the scripts dies at the die, I didn't post anything below, and everything above is just if statements to check the entered data.

Link to comment
https://forums.phpfreaks.com/topic/226683-mysqli_insert_id-doesnt-work/
Share on other sites

w3schools does not contain any mysqli specific infomration. When using  mysqli_insert_id() procedurally, the connection parameter is required.

 

In the code you posted, you are NOT executing the query, so mysqli_insert_id() will always return zero. From the php.net documentation -

Returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value.

 

Thanks for the tip, the query was working as it is, so I forgot to even execute it with mysqli_query()

 

but the problem I'm having now is, that it's ALWAYS printing out a 1, and not the actual new ID.

 

Do you think I have something wrong in the query constellation?

 

Everything gets inserted correctly in the query, and the ID is auto incrementing.

echo $lastid = mysqli_insert_id($dbc) or die (mysqli_error($dbc));

 

The use of or die() in the above line of code causes the line to be evaluated as a logical expression and the whole line/expression returns a TRUE or 1 value, which is what is echoed. You are basically echoing the fact that $lastid = mysqli_insert_id($dbc) was successful.

 

Removing the or die() would be your best bet (the place where someone suggested using that was specifically for troubleshooting why a mysqli_query() statement was not working.)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.