CodeIgniter Forums

Full Version: How to Insert Data Into Join Tables?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
I don't think MySQL allows you to do a joined insert, so you'd just do three inserts inside a transaction.
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?
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.
(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?
(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/d...tions.html
(05-26-2016, 07:22 PM)Read this part of the docs: Wrote: [ -> ]https://www.codeigniter.com/user_guide/d...tions.html

Thank you for your docs.