954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to insert and select the data from the same table?

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.

nikki05
Newbie Poster
16 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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

rohit.k2903
Newbie Poster
13 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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

nalini@121
Newbie Poster
23 posts since Nov 2011
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: