kreut Posted February 20, 2011 Share Posted February 20, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/228294-understanding-lastinsertid/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 20, 2011 Share Posted February 20, 2011 What database class are you using or what is the actual code for the lastInsertId() method? Quote Link to comment https://forums.phpfreaks.com/topic/228294-understanding-lastinsertid/#findComment-1177206 Share on other sites More sharing options...
cunoodle2 Posted February 20, 2011 Share Posted February 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228294-understanding-lastinsertid/#findComment-1177216 Share on other sites More sharing options...
Skepsis Posted February 20, 2011 Share Posted February 20, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/228294-understanding-lastinsertid/#findComment-1177246 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.