Jump to content


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

  • Please log in to reply
2 replies to this topic

#1 emehrkay

  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 11 September 2006 - 07:55 PM

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?

#2 roopurt18

  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 11 September 2006 - 07:59 PM

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.

$res = mysql_query("insert some sort of data");
$id = mysql_insert_id();
  mysql_query("insert more data using $id to link across tables");
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.
PHP Forms : Part I | Part II

JavaScript: Singleton


#3 emehrkay

  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 12 September 2006 - 01:23 AM

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

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users