views:

135

answers:

1

Hi,
I use codeigniter's database abstarction, and im doing a transaction with it. My problem is, that i have several inserts into several tables, but i need the insert id from the first insert query. Is there any way to store the last insert id for more than one following insert?

I don't understand why, but the ci built in function does not work.

+4  A: 

Just grab the insert_id right after you do the queries...

$this->db->insert('table1', $data);
$insert_id1 = $this->db->insert_id();

$this->db->insert('table2', $data);
$insert_id2 = $this->db->insert_id();

$this->db->insert('table3', $data);
$insert_id3 = $this->db->insert_id();

Its the simplest way to do it.

Tom Schlick
Ok, thanks :) I just didnt know how to use the insert_id() method.
Nort
Your welcome :)
Tom Schlick