Page 1 of 1

LAST_INSERT_ID()

Posted: Fri May 30, 2008 11:17 pm
by chopper_pc
I have a html form that passes information to a php script that builds a record in a table. I am trying to find out how to be able to get the id (primary key) of the new record. I have googled and the best I have come with is using LAST_INSERT_ID(). From what I understand that grabs the id of the last record put in any table in the database. That will not work. Really all I want to do is display the new record in a special formatted page I have built already. Any Ideas of functions that might do this or places to look?

Re: LAST_INSERT_ID()

Posted: Fri May 30, 2008 11:39 pm
by John Cartwright
mysql_insert_id() will give you the last id generated from auto increment in your connection.

Re: LAST_INSERT_ID()

Posted: Sat May 31, 2008 12:17 am
by chopper_pc
Thanks,
knowing that I now have the output url http://localhost/mypage.php?postid=81, whats the proper usage on the mypage.php to use this?
mysql_query("select * from BOL WHERE id=????

Re: LAST_INSERT_ID()

Posted: Sat May 31, 2008 10:42 am
by chopper_pc
Solved.......
$postid = $_GET['postid'];
mysql_query("select * from BOL WHERE id=$postid........
Sorry, I really need to sleep more often :banghead:

Re: LAST_INSERT_ID()

Posted: Tue Jun 03, 2008 10:21 pm
by andre_c
Whatch out for sql injection, you need to escape the parameters:

Code: Select all

$postid = mysql_real_escape_string($_GET['postid']);
or at least make sure it's a integer (if that's what it is)