December 2013
Intermediate to advanced
306 pages
6h 26m
English
Returning the Primary Key of the last inserted row can be useful in instances where you may wish to write data to more than one table and whose data may be related via the keys. CodeIgniter provides support for returning the last inserted key.
function insert($data) {
if ($this->db->insert($data, 'table_name')) {
return $this->db->last_id();
} else {
return false;
}
} Take a look at the lines in bold. We test for the returned value of $this->db->insert($data);, which will return true if successful and false if there was an error. If the returned value is true, we grab the Primary Key of the last inserted record for this connection; this ...
Read now
Unlock full access