RE: [PHP] INSERT_ID() & INSERT problem
| From: | John Holmes | Date: | Mon, 17 Jun 2002 02:02:41 +0000 |
| Subject: | RE: [PHP] INSERT_ID() & INSERT problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-102449@lists.php.net to get a copy of this message | ||
What your doing is okay. You can use LAST_INSERT_ID() in your query,
like you're doing.
What do you meant that the second query has an auto_increment column,
but only if nothing was entered? It looks like you're putting $value
into the first column, so an auto_increment column isn't going to be
generated. That's why your MySQL_insert_id() is returning the value from
your first query. You have to insert NULL, or leave the column out of
your insert statement, in order for a number to be assigned by
auto_increment.
---John Holmes...
> -----Original Message-----
> From: César Aracena [mailto:icaam@icaam.com.ar]
> Sent: Sunday, June 16, 2002 7:57 PM
> To: 'PHP General List'
> Subject: [PHP] INSERT_ID() & INSERT problem
>
> Hi all,
>
> Im 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, Im 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, its 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
>