Jal Rhee Ducusin Malugao Jal Rhee Ducusin Malugao - 1 year ago 72
SQL Question

Sql/php - insert data to a table with foreign keys

How do you code in php to insert datas to a table who have two foreign keys and these foreign keys are the primary key of different tables.

This is a many to many relationship so i am making a new table. And i want to populate this table. I searched a lot these past days on how to get the primary key of a row on the left table and right table to combine it in the middle table. But it all doesnt work. Please help.

Answer Source

You should use mysqli_insert_id() after each insert, keep each into its own variable, then use those as your FKs when inserting into the 3rd table.

It will return int. Have a look at the doc: PHP Doc

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download