[SOLVED] Finding what was entered
Moderator: General Moderators
[SOLVED] Finding what was entered
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?
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.
from mysql.com
http://dev.mysql.com/doc/mysql/en/getti ... ue-id.htmlWhen 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.
If the query is :
Then would i just use:
?
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());Code: Select all
used_id = mysql_insert_id($enterq);almost
Code: Select all
//after your query;
$sql = "SELECT LAST_INSERT_ID()";
$result = mysql_query($sql);- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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
As simple as that 
Code: Select all
$id = mysql_insert_id();- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact: