Skip to main content

Get last insert id after insert query Code-igniter

function save_student()
{
$student_data=array(
'name'=>$this->input->post('name'),
'last_name'=>$this->input->post('last_name')
);
$this->db->insert('student_db', $student_data);
$insert_id = $this->db->insert_id();
return $insert_id;
}

insert_id() function will return zero if table does not have auto incremented column so make sure table must have a column with AUTO_INCREMENT attribute.

Comments