[thelist] Sql muti table insert needing auto increment value

Steve Lewis nepolon at worlddomination.net
Fri Aug 8 13:28:57 CDT 2003

fstorr wrote:

>Hi all
>I need to do a SQL insert statement into three tables.  However, the
>second table contains a column which is a foreign key to the first
>table's auto incrementing index.  How do I insert this value into the
>second table (I'm using PHP+MySQL) when I don't actually know what it
Anthony just answered your question, but to recap what was said July 21 
after several days of discussion:

 > in order of preference, here are the techniques --
 > 1 @@identity, mysql_last_id, or whatever method the database provides
 > 2 INSERT, then SELECT back using the "real" primary key value(s)
 > 3 transaction block around INSERT, SELECT MAX(id)

> note that there is absolutely no danger of getting the wrong row back using
> method 2, and further, method 2 does not require a transaction block

> rudy



More information about the thelist mailing list