Forum Moderators: coopster

Message Too Old, No Replies

Insert row - get primary key - use it in other tables

Can this be done in one call using MySQL and PHP?

         

soladon

9:04 am on Jul 24, 2006 (gmt 0)

10+ Year Member



Hello,

I am now using php and mysql(i, in this case).

I have a form which inserts data into two related tables, A and B. The first one, A has a primary key and other simple information (no keys). Table B has a foreign key to A and another column.

As you might guess I first need to insert a new row into A and then insert data into B approprietly - that is I need to have the new primary key generated in A just before inserting data into B.

I have found that I can use the php function "mysqli_insert_id" but that would mean I'll have to make two different calls to the database.

My question is: can I make it in one call, or what is the fastest/your way of solving this very common problem.

Thank you,

coopster

10:45 am on Jul 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, soladon.

Here is another discussion that may answer your question. The last message offers details and an example.

primary key and another field the same.... how to? [webmasterworld.com]