Reputation: 73
Im trying to insert a value once its created from one table into another I have two databases 'mem' and 'location'
I want to add the primary key 'id' from the mem table and insert it into a column 'user_id' in the location table.
I have a sql query in my registration form page that auto increments the 'id' in the mem table yet does not seem to add the same value into the user_id in the location table,
$id = mysql_insert_id(); mysql_query("INSERT INTO location (user_id)
VALUES (SELECT id FROM mem)");
Can someone please help!
Upvotes: 0
Views: 567
Reputation: 6627
The proper query is:
INSERT INTO location (user_id) SELECT id FROM mem
However, this query will actually insert all id values from 'mem' into 'location'. To add the most recent id, you will want to use
INSERT INTO location (user_id) VALUES ($id)
Upvotes: 1