0

Hi,

I want to insert and select the data from the same table.I am using following query to insert and select the data from the same table. However, I am not able to insert the data.
Is there any method to do this?

$sql=mysql_query("INSERT INTO users (name,ref_no) VALUES( '$name', (SELECT id FROM users WHERE name='$name') )");


id is AUTO_INCREMENT


Would greatly appreciate your help.

Thank you.

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by nalini@121
0

Hi,

I want to insert and select the data from the same table.I am using following query to insert and select the data from the same table. However, I am not able to insert the data.
Is there any method to do this?

$sql=mysql_query("INSERT INTO users (name,ref_no) VALUES( '$name', (SELECT id FROM users WHERE name='$name') )");


id is AUTO_INCREMENT


Would greatly appreciate your help.

Thank you.

You have first insert a record and then select.
Refer: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

0

Try your query like this
$sql=mysql_query("INSERT INTO users (name,ref_no) VALUES($name, (SELECT id FROM users WHERE name='".$name."') )");

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.