Bonfire
Bonfire copied to clipboard
Insert returns false if primary key is not auto increment
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?
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
.