Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

insert in 2 tables & get value from 1st in 2nd
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Ashok
05-25-05 01:56 AM


Re: insert in 2 tables & get value from 1st in 2nd
Do 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


Report this thread to moderator Post Follow-up to this message
Old Post
Steve
05-25-05 01:56 PM


Re: insert in 2 tables & get value from 1st in 2nd
Ashok 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;

Report this thread to moderator Post Follow-up to this message
Old Post
Vincent THOREL
05-30-05 01:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP SQL archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:30 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.