Bonfire icon indicating copy to clipboard operation
Bonfire copied to clipboard

Insert returns false if primary key is not auto increment

Open josephwaiguru opened this issue 9 years ago • 1 comments

I have the id_no as the primary key but it always returns zero. Does it mean the primary key must always be auto increment?

josephwaiguru avatar Jan 19 '16 12:01 josephwaiguru

If you don't call return_insert_id(false) (or set the $return_insert_id property to false) on your model, the insert method will call $this->db->insert_id(), which, when using the MySQLi driver, will return 0 if the last query did not update an AUTO_INCREMENT value.

If you set $return_insert_id to false, it will return true on a successful insert. If you don't want to set $return_insert_id, you should still be able to differentiate between success or failure by using $this->model->insert($data) === false or $this->model->insert($data) !== false.

mwhitneysdsu avatar Jan 19 '16 14:01 mwhitneysdsu