0

Possible Duplicate:
How to get the id of a row i've just inserted php/mysql

I have a form with the fields:

  • Name
  • Email
  • City
  • State
  • Zip

and the user fills in with valid data... its sent to a (php) script and INSERTed into the db.

Is it possible to grab the auto-incrementing PK id WHEN submitted? E.g. say the PK id for that data I entered is '6' . How do I get this PK value programmatically (via php) upon submission? (Without having to call the last PK ID via SQL ? ) or is via SQL the only way?

Community
  • 1
  • 1
CodeTalk
  • 3,571
  • 16
  • 57
  • 92

4 Answers4

4

mysql_insert_id().

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

Use this PHP Function:

int mysql_insert_id ([ resource $link_identifier = NULL ] )

source:

http://php.net/manual/en/function.mysql-insert-id.php

dansanb
  • 33
  • 3
0

With mysql_insert_id

http://php.net/manual/en/function.mysql-insert-id.php

<?php
$id = mysql_insert_id();
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

Or, if you've moved on to MySQLi, $mysqli->insert_id; or mysqli_insert_id()

Umbrella
  • 4,733
  • 2
  • 22
  • 31