Jump to content

How can I properly insert latest id into table with PHP?


Go to solution Solved by boompa,

Recommended Posts

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.

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.