Jump to content

How to retrive primary key id after INSERT statement?


holy24

Recommended Posts

Hi, I have created 2 tables (table1 and table2) inside same database. There will be a primary key "id" (auto-increment). I used the INSERT statement to insert a data to table1: mysql_query ("INSERT INTO table1 (name, age) VALUES ('$_SESSION[username]', '$_SESSION[age]')"); After the above sql statement run, how can I save the "id" of that record from table1 into table2 with some other new values? $datetime=date("d/m/Y h:i:s");mysql_query ("INSERT INTO table2 (id, datetime) VALUES ('id from table1','$datetime')"); Thanks.

Link to comment
Share on other sites

Hi, I have created 2 tables (table1 and table2) inside same database. There will be a primary key "id" (auto-increment). I used the INSERT statement to insert a data to table1: mysql_query ("INSERT INTO table1 (name, age) VALUES ('$_SESSION[username]', '$_SESSION[age]')"); After the above sql statement run, how can I save the "id" of that record from table1 into table2 with some other new values? $datetime=date("d/m/Y h:i:s");mysql_query ("INSERT INTO table2 (id, datetime) VALUES ('id from table1','$datetime')"); Thanks.
easy, it have two way1. before you start insert into, you check the last id and you add that id+1, so you will know which id is have in table 1, you can then just put that id to table 22.when you insert data to table1, you search it whit all your data, example
$Check=mysql_query("SELECT * FROM table1 WHERE name="$_SESSION['username']" and age="$_SESSION['age']");if($checkid=mysql_fetch_array($Check)){$checkidfortable2=$checkid['id'];}

Link to comment
Share on other sites

1. before you start insert into, you check the last id and you add that id+1, so you will know which id is have in table 1 , you can then just put that id to table 2
That may cause problems in high-traffic multi-user systems. It's referred to as a race condition.
Link to comment
Share on other sites

you can also use $_GET[] and once you define the $_GET[] add it into your query for table2 making sure table 2 have a column for table1 id and thats should solve the problem...........*i might be wrong also*

Link to comment
Share on other sites

where did $_GET come from? The OP is talking about getting a the id of a newly inserted record into the database, to use with another query.

Edited by thescientist
Link to comment
Share on other sites

maybe every1 blind? :)Already show 2 way for that problem.
we saw, but there is already a native function for what the OP wants.
Link to comment
Share on other sites

really? then post it :)
we did. birbal had it in his post (second one of the thread), albeit the link itself was slightly "broken".
link seems broken. i think this is the one you meant.http://php.net/manua...l-insert-id.php
Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...