[SOLVED] Finding what was entered

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

[SOLVED] Finding what was entered

Post by rsmarsha »

I have a query to enter an order into a DB, then if i want to find the number that was just entered i have to do another select query.

Is there a way of returning the value of a field that was just entered into the DB from an insert query?
Last edited by rsmarsha on Thu Apr 28, 2005 6:21 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

from mysql.com
When a new AUTO_INCREMENT value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID() statement with mysql_query() and retrieving the value from the result set returned by the statement.
http://dev.mysql.com/doc/mysql/en/getti ... ue-id.html
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Great thanks, i'll look into it. :)
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

If the query is :

Code: Select all

$enterorder = "INSERT INTO orders (orderno,username,dateadded,status,archive,exvat,price,payment,cpu,memory,motherboard,usb,hdd,hdda,raid,cdrom,dvd,dvdw,gcard,gcarda,sound,modem,network,floppy,cardreader,pccase,psu,osreq,firewire,monitor,keyboard,mouse,speakers,printer,virus,office,tvcard,onsite,q,discount,total,totalexvat,deldet) VALUES ('','$mail','$datejoined','$type','','$_POST[exvat2]','$_POST[price]','Finance','$_POST[cputext]','$_POST[memtext]','$_POST[boardtext]','$_POST[usbtext]','$_POST[hddtext]','$_POST[hddatext]','$_POST[raidtext]','$_POST[cdrtext]','$_POST[dvdtext]','$_POST[dvdrwtext]','$_POST[gcardtext]','$_POST[gcardatext]','$_POST[soundtext]','$_POST[modemtext]','$_POST[nettext]','$_POST[fddtext]','$_POST[mcrtext]','$_POST[casetext]','$_POST[psutext]','$_POST[osreqtext]','$_POST[firetext]','$_POST[montext]','$_POST[keytext]','$_POST[mousetext]','$_POST[sprtext]','$_POST[pritext]','$_POST[virtext]','$_POST[offtext]','$_POST[tvctext]','$_POST[onstext]','$_POST[q]','$_POST[discount]','$_POST[final]','$_POST[extotal]','$_POST[deldet]')";
	$enterq = mysql_query($enterorder, $db_conn) or die("Query enter order failed".mysql_error());
Then would i just use:

Code: Select all

used_id = mysql_insert_id($enterq);
?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

almost

Code: Select all

//after your query;
$sql = "SELECT LAST_INSERT_ID()";
$result = mysql_query($sql);
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

mysql_insert_id() is perfectly valid and shortcut alternate to what phpScott posted above. only thing is that you have it quite in wrong way. Its quite simply 1 liner :arrow:

Code: Select all

$id = mysql_insert_id();
As simple as that ;)
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

thanks all :)
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

doh, been awhile :oops:
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

phpScott wrote:doh, been awhile :oops:
Hey, no need man. :) We all are prone to this. Computers is such a field that you tend to forget and lag behind vastly if you aren't in touch. :wink:
Post Reply