holy24 Posted November 16, 2012 Share Posted November 16, 2012 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 More sharing options...
birbal Posted November 16, 2012 Share Posted November 16, 2012 (edited) http://php.net/mysql_insert_id Edited November 16, 2012 by birbal Link to comment Share on other sites More sharing options...
phpnoob Posted November 16, 2012 Share Posted November 16, 2012 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 More sharing options...
thescientist Posted November 16, 2012 Share Posted November 16, 2012 http://php.net/mysql_insert_idlink seems broken. i think this is the one you meant.http://php.net/manual/en/function.mysql-insert-id.php Link to comment Share on other sites More sharing options...
justsomeguy Posted November 16, 2012 Share Posted November 16, 2012 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 2That may cause problems in high-traffic multi-user systems. It's referred to as a race condition. Link to comment Share on other sites More sharing options...
divinedesigns1 Posted November 16, 2012 Share Posted November 16, 2012 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 More sharing options...
thescientist Posted November 16, 2012 Share Posted November 16, 2012 (edited) 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 November 16, 2012 by thescientist Link to comment Share on other sites More sharing options...
phpnoob Posted November 16, 2012 Share Posted November 16, 2012 (edited) maybe every1 blind? :)Already show 2 way for that problem. Edited November 16, 2012 by phpnoob Link to comment Share on other sites More sharing options...
thescientist Posted November 16, 2012 Share Posted November 16, 2012 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 More sharing options...
phpnoob Posted November 16, 2012 Share Posted November 16, 2012 we saw, but there is already a native function for what the OP wants.really? then post it Link to comment Share on other sites More sharing options...
thescientist Posted November 16, 2012 Share Posted November 16, 2012 (edited) really? then post it we did. birbal had it in his post (second one of the thread), albeit the link itself was slightly "broken". http://php.net/mysql_insert_id link seems broken. i think this is the one you meant.http://php.net/manua...l-insert-id.php Edited November 16, 2012 by thescientist Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now