1

I want to insert data into two tables of mysql,

Tables are as follows:

1) t_entity_details 2) t_preferences

Columns inside the table are as follows:

t_entity_details

- Entity_id (primary key, auto-increment)
- First_Name
- Sex
- TagLine
- Personal_Desc
- DOB
- Lang

t_preferences

- Entity_id (primary key too, No auto-increment)
- other columns......

Now when I submit a form, the Entity_id should be same in both the tables. So how to do that? Please help me with this.

1
  • are you saying one form input should go into two different tables? if yes can you paste what you have done . Commented Apr 29, 2015 at 7:16

2 Answers 2

1

The Solution is simple. You can use "mysql_insert_id" to get the last incremented/inserted id. You can use that in turn to insert into your second table.

In Eloquent, things are even more simpler, Assuming you know about Eloquent Models,. You insert to the first table:

$insertArray = array('First_Name' => 'Some_value', 
    'Sex' => 'Some_value', 
    'TagLine' => 'Some_value', 
    'Personal_Desc' => 'Some_value', 
    'DOB' => 'Some_value', 
    'Lang' => 'Some_value');

$saveResult = EloquentModel::create($insertArray);

You can then get the last insert id as follows:

  $entity_id = $saveResult->id;

You use this to insert into the second table:

$insert_secondTable_array = array("Foo" => "bar");
$insert_secondTable_array['EntityId'] = $entity_id;
$result = SecondEloquentModel::create($insert_secondTable_array);

This inserts into both tables.

Sign up to request clarification or add additional context in comments.

Comments

0

In this scenario do the query statement as,

insert into t_entity_details (column1,column2,column3,...) VALUES (value1,value2,value3,...);

insert into t_preferences SELECT * FROM t_entity_details;

i believe this type will solve your problem if you don't do manipulation using other columns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.