CodeIgniter Last Inserted ID
CodeIgniter comes with a number of cool database helpers, like $this->db->insert_id() which tracks the automatic ID created for the last insert statement you ran so you can use it in subsequent code that relies on that ID.
For example:
$result = $this->db->insert('users', $user_data);
$user_id = $this->db->insert_id();
Now you have the user ID from the insert which can you then work with in your code without having to go back to the database and look it up.