CodeIgniter Forums
How to Insert Data Into Join Tables? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: How to Insert Data Into Join Tables? (/thread-65296.html)



How to Insert Data Into Join Tables? - Phally Seng - 05-25-2016

I can select and join, but I can't insert with these below tables in the same time. How to insert data into joined tables?

- tbl_category(cate_id, name, description)

- tbl_brand(brand_id, brand_name)

- tbl_product(product_id, product_name, cate_id, brand_id)


RE: How to Insert Data Into Join Tables? - skunkbad - 05-25-2016

I don't think MySQL allows you to do a joined insert, so you'd just do three inserts inside a transaction.


RE: How to Insert Data Into Join Tables? - Phally Seng - 05-26-2016

Yes, I think so. But I find this idea. How to do it in Codeigniter?

Code:
INSERT INTO table3 ( name, age, sex, city, id, number, nationality)
SELECT name, age, sex, city, p.id, number, n.nationality
FROM table1 p
INNER JOIN table2 c ON c.Id = p.Id
INNER JOIN table3 n ON n.Id = p.Id


And I also find trigger. Which one is better to avoid lost data?


RE: How to Insert Data Into Join Tables? - skunkbad - 05-26-2016

That is different. If you look carefully, what is being inserted is data that is being selected from three other tables. So it's not that the insert is a join, but the insert is OF a join.


RE: How to Insert Data Into Join Tables? - Phally Seng - 05-26-2016

(05-26-2016, 08:52 AM)skunkbad Wrote: That is different. If you look carefully, what is being inserted is data that is being selected from three other tables. So it's not that the insert is a join, but the insert is OF a join.

Thanks for your explanation. I'd need your idea, I'm sorry for my updated question. How can I insert data to multiple tables in sequence at the same time?


RE: How to Insert Data Into Join Tables? - skunkbad - 05-26-2016

(05-26-2016, 06:34 PM)Phally Seng Wrote:
(05-26-2016, 08:52 AM)skunkbad Wrote: That is different. If you look carefully, what is being inserted is data that is being selected from three other tables. So it's not that the insert is a join, but the insert is OF a join.

Thanks for your explanation. I'd need your idea, I'm sorry for my updated question. How can I insert data to multiple tables in sequence at the same time?

Read this part of the docs:

https://www.codeigniter.com/user_guide/database/transactions.html


RE: How to Insert Data Into Join Tables? - Phally Seng - 05-27-2016

(05-26-2016, 07:22 PM)Read this part of the docs: Wrote: https://www.codeigniter.com/user_guide/database/transactions.html

Thank you for your docs.