LAST_INSERT_ID()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
chopper_pc
Forum Newbie
Posts: 15
Joined: Fri May 30, 2008 10:55 pm

LAST_INSERT_ID()

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: LAST_INSERT_ID()

Post by John Cartwright »

mysql_insert_id() will give you the last id generated from auto increment in your connection.
User avatar
chopper_pc
Forum Newbie
Posts: 15
Joined: Fri May 30, 2008 10:55 pm

Re: LAST_INSERT_ID()

Post 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=????
User avatar
chopper_pc
Forum Newbie
Posts: 15
Joined: Fri May 30, 2008 10:55 pm

Re: LAST_INSERT_ID()

Post by chopper_pc »

Solved.......
$postid = $_GET['postid'];
mysql_query("select * from BOL WHERE id=$postid........
Sorry, I really need to sleep more often :banghead:
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Re: LAST_INSERT_ID()

Post 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)
Post Reply