PHP - Mysqli_insert_id
Hi,
I am trying to use mysqli_insert_id command, but still couldn't really figure out how I should apply it, would appreciate if anyone is willing to give me some advice. Thank you /**INSERT into tutor_login table**/ $query1 = "INSERT INTO tutor_login (email, password) VALUES ('$email', '$password')"; $results1 = mysqli_query($dbc, $query1) or die(mysqli_error()); //Currently, right after the insertion of email and password into tutor_login table, it should be generating a tutor_id Thereafter, how should I execute 'mysqli_insert_id' command, so that I can insert it into my $query2's tutor_id. Thanks /**INSERT into tutor_profile table**/ $query2 = "INSERT INTO tutor_profile (name, gender, tutor_id) VALUES ('$name', '$gender', '$tutor_id')"; $results2 = mysqli_query($dbc, $query2) or die(mysqli_error()); Similar TutorialsI'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. |