i have a trouble when retrieve last insert id in codeigniter. when i try to debug like var_dump(); the output just send int(0)
i use uuid as id with primary key. this is the code:
$this->db->set('id_customer','uuid_short()',FALSE);
$query = $this->db->insert('customer',$data);
$id = $this->db->insert_id();
echo var_dump($id);
if($query)
{
$array = array(
'kode_trans' => 'uuid()',
'trans_date' => 'NOW()'
);
$this->db->set('id_customer','$id');
$this->db->set($array,'',FALSE);
$this->db->insert('transaction_header');
return $id;
}else{
return FALSE;
}
im newbie in ci. there is something wrong with my code?
The "insert_id" function uses PHP's mysql_insert_id function, which returns "The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value"
You could try it this way;
$id = uuid_short();
$data['id_customer'] = $id;
$query = $this->db->insert('customer', $data);
if ( $query )
{
$array = array(
'kode_trans' => 'uuid()',
'trans_date' => 'NOW()'
);
$query = $this->db->insert('transaction_header', $array);
return $id;
}
else
{
return false;
}