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,343
  • 14
  • 52
  • 87

4 Answers4

4

mysql_insert_id().

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
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
  • 44,769
  • 7
  • 56
  • 100
0

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

Umbrella
  • 4,621
  • 2
  • 21
  • 31