skip to Main Content

Say I have two tables, ‘users’ and ‘restaurants’.

Users Table:

user_id email u_name
1 [email protected] John
2 [email protected] Jane

Restaurants table –
here owner is the foreign key referencing user_id

restaurant_id r_name address owner
1 KFC New York 1
2 Starbucks Texas 2

i have a form where the user enter details into the first table. And then i have another form to enter details into second table. how do i set the values in ‘owner’ column? The user_id and restaurant_id are auto generated. How do i know which restaurant belongs to which user?

2

Answers


  1. You can use a SELECT statement in your INSERT, like this:

    INSERT INTO restaurants(c1, c2, c3) 
    SELECT 'foo' -- new content 
    , 'bar' -- new content 
    , id_user -- primary key used for the FK constraint 
    FROM users 
    WHERE username = 'Mickey';
    
    Login or Signup to reply.
  2. In the second form(form to enter details into second table),
    a dropdown can be provided which displays list of users from table 1. dropdown usually has value and text part. value can be set to user_id and text can be set to u_name / email.
    r_name and address, you are getting from user in the second form?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search