therocker Posted December 13, 2013 Share Posted December 13, 2013 How would I insert the latest known id into a different table? Was thinking LAST_INSERT_ID(), but don't know if it's what I'm looking for. Here is my code. <?php $insert1 = $mysqli->query("INSERT INTO table1 (title) VALUES ('$title')"); $insert2 = $mysqli->query("INSERT INTO table2 (last_known, title) VALUES ('$last_id', '$title')"); ?> This is all on 1 page. I wanted to insert the id from table1 before actually querying the insert into table2 so that last_known will have the id that table1 has. My intentions is to use this as a custom message page. When someone creates a message, it should insert all of the data into table1 and at the same exact time, insert into table2 the id from table1. The ids are auto increment and will automatically be inserted. I don't want to define the ids because if I insert the new id into table1, then if someone has the URL with something like http://domain.com/message.php?id=1 would easily not be able to insert their own messages because row 1 already exists. Link to comment https://forums.phpfreaks.com/topic/284754-how-can-i-properly-insert-latest-id-into-table-with-php/ Share on other sites More sharing options...
boompa Posted December 13, 2013 Share Posted December 13, 2013 Assuming the ID of the new record is an AUTO_INCREMENT field, you can use mysqli->insert_id. Link to comment https://forums.phpfreaks.com/topic/284754-how-can-i-properly-insert-latest-id-into-table-with-php/#findComment-1462295 Share on other sites More sharing options...
therocker Posted December 13, 2013 Author Share Posted December 13, 2013 On 12/13/2013 at 8:03 PM, boompa said: Assuming the ID of the new record is an AUTO_INCREMENT field, you can use mysqli->insert_id. Thanks it worked. Link to comment https://forums.phpfreaks.com/topic/284754-how-can-i-properly-insert-latest-id-into-table-with-php/#findComment-1462296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.