homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Insert new IDs as Foreign Keys in 2nd tab right after they got written
Problem with writing multiple foreign keys.
erroronexit




msg:4028519
 3:18 am on Nov 20, 2009 (gmt 0)

I am new to PHP, not sure this belongs to PHP or MySQL. Here is my scenario:

I have multiple data from a form search that I want to write to 2 tables. So far that's no problem.

But the incremental IDs (plural!) that are created in TABLE1.ID should be inserted as foreign keys in TABLE2.ID_T1

My first thought was that mysql_insert_id()or last_insert_id() would come in handy - but they just can handle one ID, but I insert several.

Does anybody has clue how to work this out?

 

da_pinky




msg:4028572
 5:45 am on Nov 20, 2009 (gmt 0)

not sure what you want to do. could you not append each last_insert_id to an array and then do the second batch of inserts?

erroronexit




msg:4028578
 6:09 am on Nov 20, 2009 (gmt 0)

I did a workaround now. I created a new field in TABLE1 and inserted there the source ID. Then I take the incremented TABLE1.ID and put it into TABLE2 as foreign ID. Seems to work.


$sql = "INSERT INTO table1 (field1)VALUES
('" .implode("'), ('", $_POST['checkbox']) . "')";

mysql_query($sql) or exit(mysql_error());

$sql = "INSERT INTO table2 (id_t1)
SELECT table1.id FROM table1
WHERE field1 IN ('" .implode("', '", $_POST['checkbox']) . "') ";

mysql_query($sql);

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved