Jump to content

Understanding lastInsertId()


kreut

Recommended Posts

Hello,

 

After a user gets input into my Users table in my database, I'm using lastInsertId() to grab the newly created ID and then enter the newly created ID (along with some other stuff) into a separate table.  Could this potentially be a problem?  In other words, what happens if 2 users on 2 different computers both sign up a the same time?  How will be database know which lastInsertId() to use?  Here's my current code:

 

$dbWrite->insert('users', $data);
  $last_id = $dbWrite -> lastInsertId();                        // get the ID that was just created
   $data = array ('course_id' => $_POST['course_id'],
   				  'user_id' => $last_id);
	$dbWrite->insert('course_enrollment', $data); // Use that new ID in a different table 

 

Perhaps there's a way to combine the steps?

 

Thank you for your thoughts...

Link to comment
Share on other sites

I use an "Insert" function that simply returns false OR the record that was inserted.

 

There is quite a bit more to this but the function is essentially this..

 

<?php
function Insert($query,$data)
{
$db_conn = new PDO(DB_CONN, DB_USER, DB_PASS);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//prepare startment for inserting a record into the DB
$insert_stmt = $db_conn->prepare($query);
$insert_stmt->execute($data);
$last_insert_id = $db_conn->lastInsertId();

//close up the DB connection to save on memory
$db_conn = NULL;
return $last_insert_id;
}
?>

 

Again that is only about 20% of the function.  You should be using try/catch and error checking to make sure that everything works correctly.

Link to comment
Share on other sites

We just need to see function lastInsertId();.

function mysql_insert_id(); will be your friend in this case.If your lastInsertId function does not contain this code,mysql_insert_id();Then you can delete $last_insert_id = $db_conn->lastInsertId();

 

and changereturn $last_insert_id;

to return mysql_insert_id();

 

mysql_insert_id checks to see what the id of that insert would be.

 

So when calling the query, you would do,

 

$latest_id = $dbWrite->insert('course_enrollment', $data);$dbWrite->insert('course_enrollment', $data);

if you echo $lastest_id, it will be the id of the item you just created, i'm pretty sure this code is absolutely fine even if two people post something at the same time.Just remember, if lastInsertId(); contains the mysql_insert_id code you are already where you want to be, and don't need to really worry about the two people thing.

[/color][/color][/color][/color]

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.