Home » Php » Insert into two table foreign key and primary key

Insert into two table foreign key and primary key

Posted by: admin September 25, 2018 Leave a comment

Questions:

I have two tables in my database namely reservations and guests. The rows are as follow:

Reservation:

  • Reservation id
  • Property Id
  • Checkin date
  • Checkout date
  • Price

Guest

  • Guest id
  • Reservation id
  • Guest name
  • Guest address

Using a form that contains the reservation details and the guest details at the same time.

How can I run the insert query so that the auto generated reservation id(auto-increment in database) can also be inserted into the guest table at the same time?

This is what I have so far

INSERT INTO reservations
   SET Checkin date= '24/05/2018', checkout date = '29/05/2018';
INSERT INTO guests 
   SET reservation id = LAST_INSERT_ID(), 
       guest name = Some name
Answers: