title: Codeigniter:How to get the last inserted id?
body: <p>Hi I am using codeigniter.I need to get the last inserted id and increment it and use it in my view page,
  My controller code:</p>
  
  <pre><code>public function add_tickets()
       {
  
  
          $status = $this-&gt;input-&gt;post("status_button");
          $emp_array = $this-&gt;input-&gt;post("employee");
          $start_array = $this-&gt;input-&gt;post("start_time");
          $pid_array = $this-&gt;input-&gt;post("pid");
          $total_array = $this-&gt;input-&gt;post("total");
          if($status == "leave_open")
          {
              $button_status="open";
          }
          else
          {
              $button_status="";
          }
         $insert_id = $this-&gt;billing_model-&gt;store_bill($data_to_store);
        /*Here I am tring to get the last inserted id*/
      for ($i = 0; $i &lt; count($pid_array); $i++) {
          if(isset($pid_array[$i])&amp;&amp; $pid_array[$i]!=""){
          $data_to_store = array(
          'id' =&gt; $insert_id +1,
          'employee' =&gt; $emp_array[$i],
           'start_time' =&gt; $start_array[$i],
           'pid' =&gt; $pid_array[$i],
  
           'total' =&gt; $total_array[$i],
           'status' =&gt; $button_status,
           );
  
          $this-&gt;billing_model-&gt;store_bill($data_to_store);
         }
      }
      $data['ticket_new_id'] = $data_to_store['id'];
      $data['bill']=$this-&gt;billing_model-&gt;get_bill();
          $data['main_content'] = 'admin/billing/list';
          $this-&gt;load-&gt;view('includes/template', $data);  
  
       }
  </code></pre>
  
  <p>This is my controller function where I insert my bill.
  Here is my model function,</p>
  
  <pre><code>function store_bill($data)
      {
          $insert = $this-&gt;db-&gt;insert('bill', $data);
           $insert_id = $this-&gt;db-&gt;insert_id();
          return $insert_id;
  
      }
  </code></pre>
  
  <p>Here I am using <code>$this-&gt;db-&gt;insert_id()</code> to get the last inserted id.
  I am getting a error like this,</p>
  
  <pre><code>You must use the "set" method to update an entry.
  
  Filename: C:\xampp\htdocs\elfanto\elfanto_billing\system\database\DB_active_rec.php
  
  Line Number: 1174
  </code></pre>
  
  <p>Can someone help me? Thanks in advance</p>