Re: INSERT_ID() & INSERT problem
| From: | remery at emeryloftus dot com | Date: | Mon, 17 Jun 2002 00:51:03 +0000 |
| Subject: | Re: INSERT_ID() & INSERT problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-102441@lists.php.net to get a copy of this message | ||
mysql_insert_id() is SUPPOSED to return the aut-incremented value from the first query so that it
may be used for the second query. I recommend the following:
$query1 = "INSERT INTO table1 VALUES (NULL, '".$value."',
password('".$value."'), '1', '".$value."',
'".$value."')";
$result1 = mysql_query($query1);
echo "1st ADDED OK";
$my_id = mysql_insert_id();
if ($result1)
{
$query2 = "INSERT INTO table2 VALUES ('".$value."',
'".$value."',
$my_id, '".$value."', '".$value."')";
$result2 = mysql_query($query2);
what are you trying to do with this? What were you expectibng?:
$id2 = mysql_insert_id();
----- Original Message -----
From: "César Aracena" <icaam@icaam.com.ar>
To: "'PHP General List'" <php-general@lists.php.net>
Sent: Sunday, June 16, 2002 6:57 PM
Subject: [PHP] INSERT_ID() & INSERT problem
Hi all,
I'm having problems adding a new user into two tables. Actually, these
are two different tables, with different field in them. In the first
one, the user is added with an auto_increment id in order to be
recognized as a user for the web interface with a level of 1. In the
second table, the same user is added for record keeping, in which one of
its fields is the id auto-generated in the last query. I am able to
accomplish the first step of the process and insert the user into the
web interface table, but when it comes to the second part, nothing
happens.
Also, I'm using the LAST_INSERT_ID() to fetch the auto-generated number
at step 1 and that seems ok (if I print it on the screen, it gives me
the right one) but when it comes to get the mysql_insert_id() at step 2,
it also gives me the auto-generated number from the first step. ???
I have the following:
$query1 = "INSERT INTO table1 VALUES (NULL, '".$value."',
password('".$value."'), '1', '".$value."',
'".$value."')";
$result1 = mysql_query($query1);
echo "1st ADDED OK";
if ($result1)
{
$query2 = "INSERT INTO table2 VALUES ('".$value."',
'".$value."',
LAST_INSERT_ID(), '".$value."', '".$value."')";
$result2 = mysql_query($query2);
$id2 = mysql_insert_id();
echo "2nd ADDED OK with id = $id2";
}
Also, the first value entered into the second table, it's also
auto_increment but only if no input was made. ¿Is this ok?
Thanks in advance,
Cesar Aracena <mailto:webmaster@icaam.com.ar>
CE / MCSE+I
Neuquen, Argentina
+54.299.6356688
+54.299.4466621