user2003341
user2003341

Reputation: 73

Inserting value from one table into another

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

Answers (1)

Brett Wolfington
Brett Wolfington

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

Related Questions