How to Retrieve the Last Inserted ID After an INSERT Query in CodeIgniter Active Record
Problem:
When executing an insert query using CodeIgniter's Active Record, you aim to retrieve the last auto-incremented ID for the inserted record. However, you encounter difficulties obtaining this value.
Solution:
To correctly retrieve the last inserted ID in CodeIgniter Active Record, follow these steps:
Updated Model Function:
<code class="php">function add_post($post_data) { $this->db->insert('posts', $post_data); $insert_id = $this->db->insert_id(); return $insert_id; }</code>
For multiple inserts, consider using bulk inserts with batch statements.
The above is the detailed content of How to Retrieve the Last Inserted ID After an INSERT Query in CodeIgniter Active Record?. For more information, please follow other related articles on the PHP Chinese website!