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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.