0

I have 2 separate forms. The first form handler determines if the second form is needed. Each form populates a separate database table. I need to be able to link corresponding rows later. I am trying to do so by calling SCOPE_IDENTITY() after the first form and setting it to a variable to be used as the id in the second form.

I hope that made sense. Here's my code:

 $iquery = mysql_query("INSERT INTO pending (date, ip) VALUES('".$_POST['date']."', '".$_POST['ip']."')");
$asdf = SCOPE_IDENTITY();
echo $asdf;

Which give gives me this:

Fatal error: Call to undefined function scope_identity() in /public_html/test/index.php on line 138

What is the correct way to do this? keep in mind I need the result to be set as a variable.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
cream
  • 1,129
  • 5
  • 16
  • 26

1 Answers1

1

Use mysql_insert_id():

$asdf = mysql_insert_id();

or in PDO (which I'm advising you to move, here is a nice tutorial):

$asdf = $db->lastInsertId();
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • i was sure that would work, sounded so promising but instead i got this `mysql_insert_id(): supplied argument is not a valid MySQL-Link resource` – cream Oct 04 '12 at 07:55
  • lorga nevermind, i removed $iquery from the parenthesis and it's working brilliantly, thank you! – cream Oct 04 '12 at 07:59