Welcome Guest, Not a member yet? Register   Sign In
inserting data problem
#1

[eluser]september28[/eluser]
Hi there,

This may be a real noob question but here goes...

im inserting data into a table. lets say the "posts" table has 2 fields... "id" and "post". the id is an auto incrementing number in mysql.

I then want to insert into another table called "post_countries" with the fields "post_id" and "country".

I want this to happen "at the same time" or sequentially (because the user just fills out one form even tho the data goes to 2 different places)

now, what i cant figure out is how to get the post "id" on inserting it into the table (in order to then use it to insert into the post_countries table.

Does anyone have any suggestions on how to do this? Am i being really stupid?!

Cheers,

Dan
#2

[eluser]TWP Marketing[/eluser]
September28
I use this:
Code:
$this->db->insert( 'order_info', $data['order_info'] );
$Order_Info_ID = $this->db->insert_id();
The helper insert_id() is documented in the User Guide under database/helper utilities.
TWP
#3

[eluser]cideveloper[/eluser]
This should work

Code:
$data = array(
               'post' => 'Whatever Post'
            );

$this->db->insert('posts', $data);

$last_insert_id = $this->db->insert_id();

$data2 = array(
               'post_id' => $last_insert_id,
               'country' => 'Whatever Country'
            );

$this->db->insert('post_countries', $data2);




Theme © iAndrew 2016 - Forum software by © MyBB