B34ST Posted December 10, 2008 Share Posted December 10, 2008 How could I insert data into 3 Mysql tables at the same time? I have 3 tables table1 has an id field which is auto increment and a few other fields. I want to be able to insert data into this table and then retrieve the value from the id field and insert it into the other two tables along with the relevant data for the other fields. I initially thought that this could be done in one query but when i searched for this the nearest I could find was saying to use left_join but I didnt understand how this worked. thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/136289-solved-insert-data-into-3-tables-at-the-same-time/ Share on other sites More sharing options...
chronister Posted December 10, 2008 Share Posted December 10, 2008 You will use 3 separate queries. <?php $query1 = "INSERT INTO table1 (field1,field2,field3) values ($value1,$value2,$value3)"; $lastId = mysql_insert_id(); $query2 = "INSERT INTO table2 (autoIdField,field4,field5,field6) values ($lastId,$field4,$field5)"; $query3 = "INSERT INTO table3 (autoIdField,field7,field8,field9) values ($lastId, $field7,$field8,$field9)"; ?> I think I forgot a few quotes and such, but the basic principle applies. Nate $lastId will contain the auto-increment id created in the first query. Quote Link to comment https://forums.phpfreaks.com/topic/136289-solved-insert-data-into-3-tables-at-the-same-time/#findComment-710996 Share on other sites More sharing options...
B34ST Posted December 10, 2008 Author Share Posted December 10, 2008 Thanks i'll give it a try Quote Link to comment https://forums.phpfreaks.com/topic/136289-solved-insert-data-into-3-tables-at-the-same-time/#findComment-710999 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 <?php $query1 = mysql_query("INSERT INTO table1 (field1,field2,field3) values ($value1,$value2,$value3)"); $lastId = mysql_insert_id(); $query2 = mysql_query("INSERT INTO table2 (autoIdField,field4,field5,field6) values ($lastId,$field4,$field5)"); $query3 = mysql_query("INSERT INTO table3 (autoIdField,field7,field8,field9) values ($lastId, $field7,$field8,$field9)"); ?> You need to also run mysql_query Quote Link to comment https://forums.phpfreaks.com/topic/136289-solved-insert-data-into-3-tables-at-the-same-time/#findComment-711000 Share on other sites More sharing options...
chronister Posted December 10, 2008 Share Posted December 10, 2008 You need to also run mysql_query() hehehe.. I forgot that part didn't I.. I was just trying to illustrate the whole 3 separate queries and using mysql_insert_id()..... I guess I should have shown the mysql_query part too.. sorry about that. Nate Quote Link to comment https://forums.phpfreaks.com/topic/136289-solved-insert-data-into-3-tables-at-the-same-time/#findComment-711026 Share on other sites More sharing options...
B34ST Posted December 12, 2008 Author Share Posted December 12, 2008 not a problem u helped me with the general concept and as you said in your first post basic principle applies thanks again Quote Link to comment https://forums.phpfreaks.com/topic/136289-solved-insert-data-into-3-tables-at-the-same-time/#findComment-713606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.