chaseman Posted February 4, 2011 Share Posted February 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2011 Share Posted February 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 4, 2011 Author Share Posted February 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 4, 2011 Share Posted February 4, 2011 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.) Quote Link to comment Share on other sites More sharing options...
chaseman Posted February 4, 2011 Author Share Posted February 4, 2011 Ohhh I didn't even know that, I removed the or die and now it's working as expected, thank you a lot for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.