I will explain to you how to get the last inserted id in Codeigniter. Here is a simple code example to get last inserted. 


$this->db->insert_id();

Let's understand through an example create TestMd put the following code to insert data in contact_us table in the database and get the last inserted ID.

application/models/TestMd

<?php

class TestMd {

    public function getContacst($name, $email, $msg, $webisite, $phone) {

        $date = new DateTime();
        $current_date = $date->format('Y/m/d H:i:s');

        if ($this->check_msg($msg, $ip) == 0) {
            $this->db->query("INSERT INTO "
                    . "`contact_us`(`name`, `email`,`msg`, "
                    . "`date_of`, `phone`)"
                    . " VALUES ('$name','$email','$msg', "
                    . "'$current_date', '$phone')");

            //this will reture last insert ID
            return $this->db->insert_id();
            ;
        }
    }

}
?>