Jump to content


Photo

Inserting linked data into multiple tables at once


  • Please log in to reply
1 reply to this topic

#1 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 16 September 2006 - 07:25 PM

Hi I have two tables in a parent - child relationship and I'd like to be able to insert to them both with a single query.  If that isn't possible please just say so, if it is possible let me know!  Here are what my tables look like...

parent table
id
title
date_added
status

child table
parent_id
some_other_data

I want to be able to insert the data into the parent and then take the unique key id from that and place it into the child table along with any other data.  I can see how this can be done in two queries, however it seems like two queries is a lot, especially considering this is a frequently run insert.  So, is there any way to somehow get the inserted rows id from the parent table and push it into the child table all in one step?
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 16 September 2006 - 11:26 PM

Can't do that... one insert per table, then just grab the LAST_INSERT_ID().
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users