mysqlsqlmysql-insert-id

double insert with relation


I have two tables.

Table1 is named "titles" and has 3 columns: id, pagetitle, pages_id
Table2 is named "pages" and has 2 columns: id, content

In a form I am submitting 2 form fields: title, content

Now, I want to insert each value in each table, but of course I want to make sure that titles.pages_id is the same value as pages.id. Both titles.id and pages.id are auto-increment.

This is not a PHP question

I know how to do this using PHP by making two separate sql statements and using mysqli_insert_id() from the first query in the second one.

But I think there is a SQL-only way of doing a multi-insert query... or is there?


Solution

  • The auto increment won't stop you from inserting a specific id. just insert into one table first and use that id when inserting into the 2nd table.