Search code examples
phpmysqlcodeigniterlastinsertid

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?


Solution

  • $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);