genzedu777 Posted November 29, 2010 Share Posted November 29, 2010 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()); Quote Link to comment Share on other sites More sharing options...
intellix Posted November 29, 2010 Share Posted November 29, 2010 If you have a table with a column that auto increments... like for example if the 'tutor_login' table is like tutor_login_id (auto increment), email, password As soon as you've executed your INSERT query, run it afterwards using your database link and it will give you the ID of the latest thing added... like so: $id = mysqli_insert_id($dbc); Quote Link to comment Share on other sites More sharing options...
genzedu777 Posted November 29, 2010 Author Share Posted November 29, 2010 Hi intellix, Thank you the advice, but I'm only able to grasp 50% of what you meant, is it something like this below? Yes tutor_id is auto_incremental /**INSERT into tutor_login table**/ $query1 = "INSERT INTO tutor_login (email, password) VALUES ('$email', '$password')"; $results1 = mysqli_query($dbc, $query1) or die(mysqli_error()); $tutor_id=mysqli_insert_id($dbc); /**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()); Quote Link to comment Share on other sites More sharing options...
intellix Posted November 29, 2010 Share Posted November 29, 2010 Yep at a glance that should do it It's ok... nobody out of England understands me 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.