Jump to content

how do i update multiple tables at once....


emehrkay

Recommended Posts

where the key that connects them is auto_incriminated and i have no idea what it will be?

i have a contact info with a contact_info_id field (this is the main field, but unknown)

i want to insert a new contact_info record and update a few other tables at the same time, is that possible?
Link to comment
Share on other sites

When you insert into a table, if there is an auto increment field you can call mysql_insert_id() to get the id of the last value inserted.  You can then use that to insert into other tables and link data together.

[code]
$res = mysql_query("insert some sort of data");
$id = mysql_insert_id();
if($id){
  mysql_query("insert more data using $id to link across tables");
}
[/code]
That's just a quick snippet written from memory.  You'll want to check the PHP manual for the specific usages of any functions I've used, in case I misused them.

(EDIT) Alternatively you can check the MySQL documentation and see if there is a variation of the INSERT or UPDATE statements that will do this.  I don't think they exist though.
Link to comment
Share on other sites

thanks, i may have to use that, but i wanted it to be a single query because i have to give a bunch of them to someone and they'll just run them. I dont know which order they'll run them in so...

i was thinking that i could do something like

UPDATE table1 t1 SET field = value, (INSERT INTO table2 t2 (field) VALUES ('vaue)), field2 = t2.id WHERE t1.id = 'xxx'

ill try it tomorrow at work
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.