Jump to content

mysqli_insert_id


genzedu777

Recommended Posts

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());

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());

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.