get record id straight after inserting (1 Viewer)

amrick

New member
Local time
Today, 23:35
Joined
Dec 28, 2007
Messages
6
Hey all,

This is what im trying to do.

step1: User enters data into a form (done)
step2: Enter details into database (done) & redirect to page showing the data just entered so that user can confirm details. (problem)


Here is the code where the problem lies. (the sql to insert isnt shown, but is on the same php page (additem_proc.php) )
PHP:
$result = mysql_query("SELECT * FROM $tbl_name WHERE listingtitle = $listingtitle"); //Query to find listing id of record just inserted
$row = mysql_fetch_array($result);
$listingid=$row['listingid'];
header( "Location: additem_confirm.php?listingid=$listingid" ); //redirect to the newly uploaded listing

For some reason this does not find the listingid from the record that was inserted in the same page. I have tried using different parameters after the WHERE clause but with no luck. The listing id just appears as blank in the next page (additem_confirm.php?listingid=<nothinghere>")

Any obvious problems? or can someone point me in the right direction with requerying inserted data.

Thanks in advance!
 

DanG

Registered User.
Local time
Today, 15:35
Joined
Nov 4, 2004
Messages
477
Wouldn't it be easier to use sessions?
 

mark_johny

New member
Local time
Today, 15:35
Joined
Jan 4, 2008
Messages
1
You can use the php function mysql_insert_id() to get the id mysql id of the last inserted record through that database connection irrespective of the table.

For eg, to the table customers(id, name)
db_query("insert into customers set name='myname'");
$id = mysql_insert_id();

/** $id will have the value of the field id in the database for this new record.*/

Hope this would help you.
-mark

Hey all,

This is what im trying to do.

step1: User enters data into a form (done)
step2: Enter details into database (done) & redirect to page showing the data just entered so that user can confirm details. (problem)


Here is the code where the problem lies. (the sql to insert isnt shown, but is on the same php page (additem_proc.php) )
PHP:
$result = mysql_query("SELECT * FROM $tbl_name WHERE listingtitle = $listingtitle"); //Query to find listing id of record just inserted
$row = mysql_fetch_array($result);
$listingid=$row['listingid'];
header( "Location: additem_confirm.php?listingid=$listingid" ); //redirect to the newly uploaded listing

For some reason this does not find the listingid from the record that was inserted in the same page. I have tried using different parameters after the WHERE clause but with no luck. The listing id just appears as blank in the next page (additem_confirm.php?listingid=<nothinghere>")

Any obvious problems? or can someone point me in the right direction with requerying inserted data.

Thanks in advance!
uk wholesale
 

DanG

Registered User.
Local time
Today, 15:35
Joined
Nov 4, 2004
Messages
477
mark,

If you have a busy site don't you run the risk of picking up the wrong id?

That's why I thought about sessions as that insures an accurate result.
 

amrick

New member
Local time
Today, 23:35
Joined
Dec 28, 2007
Messages
6
You can use the php function mysql_insert_id() to get the id mysql id of the last inserted record through that database connection irrespective of the table.

For eg, to the table customers(id, name)
db_query("insert into customers set name='myname'");
$id = mysql_insert_id();

/** $id will have the value of the field id in the database for this new record.*/

Hope this would help you.
-mark

Mark thanks very much for that! it worked.

I found that for the scale of project i am implementing, its easier to use $id = mysql_insert_id(); rather than sessions!
 

Users who are viewing this thread

Top Bottom