Jump to content

Archived

This topic is now archived and is closed to further replies.

emehrkay

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

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?

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.