esafwan
esafwan

Reputation: 18009

Get last Inserted 'Id' while doing multiple entry to mysql in codeigniter

In my form, there are values to be inserted to multiple tables. After inserting to the first table, i have to insert the other values, along with the 'id' of the entry to first table as reference. What is the best way to do this? Is there any codeigniter specific way?

Upvotes: 2

Views: 2748

Answers (2)

stormdrain
stormdrain

Reputation: 7895

$this->db->insert_id() may be what you're looking for. Here's an example of how it could work:

$this->db->insert('Table1',$values);    
$table1_id=$this->db->insert_id();
$otherValues=array(
    'table1_id'=>$table1_id,
    );
$this->db->insert('otherTable',$otherValues);

Upvotes: 5

Alex Pliutau
Alex Pliutau

Reputation: 21957

Try to use mysql_insert_id ();

Upvotes: 1

Related Questions