user11044740
user11044740

Reputation: 21

CodeIgniter $this->db get last primary key

I want to get last primary key the table name is invoice_main I am trying to this way

$in=$this->db->insert_id();
redirect(base_url()."home/index/".$in); 

I past the code after $print = $this->db->insert('invoice_main',$datas); this line

Public function insert_invoice()
        {

          $item_name = $_POST['item'];
          $rate = $_POST['rate'];
          $quantity = $_POST['quantity'];
          $tax = $_POST['tax'];
          $amount = $_POST['amount'];


          $invoice_id = $this->input->post('invoice_id');
          //$invoice_id++;
          $datas = array(
            'user_id' => $this->session->userdata('user_id'),
               'invoice_id' => $this->input->post('invoice_id'),
             'pt_opnum' => $this->input->post('pt_opnum'),
             'pt_uhid' => $this->input->post('pt_uhid'),
             'doc_name' => $this->input->post('doc_name'),
             'status'=>1
            );
          $print = $this->db->insert('invoice_main',$datas);
          for ($i=0; $i <count($item_name) ; $i++) {
            $data=array(
                'invoice_id' =>$invoice_id,
                'pt_name'=> $this->input->post('pt_opnum'),
                'date'=>date('d-m-Y'),
                'name'=>$item_name[$i],
                'rate'=>$rate[$i],
                'quantity'=>$quantity[$i],
                'tax'=>$tax[$i],
                'amount'=>$amount[$i],
                'sub_total'=>$_POST['sub_total'],
                'o_tax'=>$_POST['o_tax'],
                'grand_total'=>$_POST['grand_total'],
                'status'=>1
                );

            $this->db->insert('invoice_details',$data);


        }


        return($this->db->affected_rows()!=1)?false:true;


    }

Upvotes: 2

Views: 159

Answers (2)

Faizan Khattak
Faizan Khattak

Reputation: 882

$this->db->insert('table_name',data);
$last_insert_id= $this->db->insert_id();

Upvotes: 1

Steve Good Job
Steve Good Job

Reputation: 136

i Think $pirnt is not return a value. to geting insert is success or not You should use :

$this->db->insert('table_name',data)
$isSuccess = $this->db->insert_id();

if($isSuccess)
{
 echo 'data updated with id : '.$isSuccess;
}else{
echo 'failed error';
}

Upvotes: 1

Related Questions