Forum Moderators: coopster

Message Too Old, No Replies

mysql insert id();

         

paseo

3:36 pm on Feb 4, 2008 (gmt 0)

10+ Year Member



Hey everybody. Got a question that shouldn't even be posing a problem but it is...:)

I am trying to use the mysql_insert_id(); to obtain the value of the last (AUTO_INCREMENT) value for (in my case) the id field. What i am doing is separating a form into two peices. The first page collects the 1st set of data and the second page adds on with more information for the same record. When the first page is submitted, the sql query is to INSERT into table...on the second page, the sql query is to UPDATE table...

The problem I am having is that the UPDATE command actually creates a new row and doesnt actually update the existing row created from the first page INSERT. I am using a WHERE statement to specify the last_ insert_id. WHERE id='".(int)$_SESSION['last_insert_id']."'";

Any ideas as to why its creating two rows as opposed to just one?

Here is the code snippet i am using:

********************************************************************

<?php

session_start();

$_SESSION['last_insert_id'] = mysql_insert_id();

include ("/db.php");

mysql_connect("$db_ip","$db_user","$db_pass");
mysql_select_db("$db_name");

$_SESSION['firstname'] = mysql_real_escape_string($_POST['firstname']);
$_SESSION['lastname'] = mysql_real_escape_string($_POST['lastname']);

$sql = "UPDATE table SET firstname='".$_SESSION['firstname']."', lastname='".$_SESSION['lastname']."' WHERE id='".(int)$_SESSION['last_insert_id']."'";

joelgreen

4:57 pm on Feb 4, 2008 (gmt 0)

10+ Year Member



[php.net...] - Get the ID generated from the previous INSERT operation

I think it can only retrieve last insert id if you do INSERT in the same request, and you have to connect to DB prior to using mysql_insert_id.

So you have to store last insert insert id into session after INSERT operation. Like this:

mysql_query("INSERT INTO mytable (product) values ('kossu')");
$_SESSION['last_insert_id'] = mysql_insert_id();

after that you'll be able to use it wherever you need.

paseo

6:06 pm on Feb 4, 2008 (gmt 0)

10+ Year Member



I have tried moving the "$_SESSION['last_insert_id'] = mysql_insert_id();" so its after the insert query and have also tried moving it the second file RF.PHP. Still no dice. Here is the complete code (2 files) stripped down to only include required information:

*****R.PHP****

<?php

session_start();

if (isset($_POST['submit']))
{

include ("/db.php");

$ru = mysql_real_escape_string($_POST['username']);
$rp = mysql_real_escape_string($_POST['password']);

$sql = "INSERT INTO users (username,password) VALUES ('$ru','$rp')";

mysql_query($sql) or die("SQL: $sql<br />".mysql_error());

$_SESSION['last_insert_id'] = mysql_insert_id();

include ("/rf.php");
exit;
}

?>

****RF.PHP*****

<?php

session_start();

if (isset($_POST['submit']))
{

include ("/db.php");

$_SESSION['r_firstname'] = mysql_real_escape_string($_POST['firstname']);

$sql = "UPDATE users SET
firstname='{$_SESSION['r_firstname']}'
WHERE id='{$_SESSION['last_insert_id']}'";

mysql_query($sql) or die("SQL: $sql<br />".mysql_error());

?>

jatar_k

11:03 pm on Feb 4, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try echo'ing the value from mysql_insert_id and see if it is returning anything

my guess would be that your update is not getting the id, so we need to figure out why

joelgreen

11:43 am on Feb 5, 2008 (gmt 0)

10+ Year Member



UPDATE command actually creates a new row

is it possible? I know REPLACE can create record, INSERT can create record. But UPDATE... strange.