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
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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.