Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I want to insert values in 2 tables from a form, but in the second table, I want to get the (new) id (auto_incr) of the first table in one of the column in 2nd table. Is it at all possible and how to do this if possible? Thanks. Ashok.
Post Follow-up to this messageDo this in 3 steps... (1) INSERT into the first table. (2) Use SELECT LAST_INSERT_ID() to get the value of the auto-increment column in the new record. (3) INSERT into the second substuting in the value from step (2). Example (error checking omitted): // (1) mysql_query( 'INSERT INTO TABLE_1 VALUES ( "aaa", "bbb" )', $SQLConnection ); // (2) $res = mysql_query( 'SELECT LAST_INSERT_ID() AS "ID"', $SQLConnection ); $row = mysql_fetch_array( $res, MYSQL_ASSOC ); $newid = $t_datRow[ "ID" ]; // (3) mysql_query( 'INSERT INTO TABLE_2 VALUES ( ' . $newid . ', "ccc" )', $SQLConnection ); --- Steve
Post Follow-up to this messageAshok wrote: > Hi, > I want to insert values in 2 tables from a form, but in the second table, I > want to get the (new) id (auto_incr) of the first table in one of the colu mn > in 2nd table. > Is it at all possible and how to do this if possible? > > Thanks. > Ashok. > > BEGIN TRANSACTION; INSERT INTO table1 VALUES (...); INSERT INTO5 table2 VALUES (LAST_INSERT_ID(), ...); COMMIT;
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.