Codeigniter:如何获取最后插入的ID?

时间:2015-09-26 11:38:17

标签: mysql codeigniter

您好我正在使用codeigniter.I需要获取最后插入的ID并将其递增并在我的视图页面中使用它, 我的控制器代码:

public function add_tickets()
     {


        $status = $this->input->post("status_button");
        $emp_array = $this->input->post("employee");
        $start_array = $this->input->post("start_time");
        $pid_array = $this->input->post("pid");
        $total_array = $this->input->post("total");
        if($status == "leave_open")
        {
            $button_status="open";
        }
        else
        {
            $button_status="";
        }
       $insert_id = $this->billing_model->store_bill($data_to_store);
      /*Here I am tring to get the last inserted id*/
    for ($i = 0; $i < count($pid_array); $i++) {
        if(isset($pid_array[$i])&& $pid_array[$i]!=""){
        $data_to_store = array(
        'id' => $insert_id +1,
        'employee' => $emp_array[$i],
         'start_time' => $start_array[$i],
         'pid' => $pid_array[$i],

         'total' => $total_array[$i],
         'status' => $button_status,
         );

        $this->billing_model->store_bill($data_to_store);
       }
    }
    $data['ticket_new_id'] = $data_to_store['id'];
    $data['bill']=$this->billing_model->get_bill();
        $data['main_content'] = 'admin/billing/list';
        $this->load->view('includes/template', $data);  

     }

这是我的控制器功能,我插入帐单。 这是我的模型函数,

function store_bill($data)
    {
        $insert = $this->db->insert('bill', $data);
         $insert_id = $this->db->insert_id();
        return $insert_id;

    }

这里我使用$this->db->insert_id()来获取最后插入的ID。 我收到这样的错误,

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

有人能帮助我吗?提前致谢

2 个答案:

答案 0 :(得分:2)

我认为这是你的解决方案:

<?php 

public function add_tickets()
     {


        $status = $this->input->post("status_button");
        $emp_array = $this->input->post("employee");
        $start_array = $this->input->post("start_time");
        $pid_array = $this->input->post("pid");
        $total_array = $this->input->post("total");
        if($status == "leave_open")
        {
            $button_status="open";
        }
        else
        {
            $button_status="";
        }

        /*beore inserting first get the last ID of the table*/

            $this->db->select('*');
            $this->db->from('bill');
            $this->db->order_by('id','desc');
            $result = $this->db->get()->result();

            $last_id = $result[0]->id;//This is the last ID of the table


      /*now insert the data with incremented ID and send it to your view */




$data_to_store = array(
    'id' => $insert_id +1,
    'employee' => $emp_array,
     'start_time' => $start_array,
     'pid' => $pid_array,

     'total' => $total_array,
     'status' => $button_status,
     );


    $this->billing_model->store_bill($data_to_store);

 $insert_id =  $last_id + 1;//this will be your last inserted ID
$data['ticket_new_id'] = $insert_id ;
$data['bill']=$this->billing_model->get_bill();
    $data['main_content'] = 'admin/billing/list';
    $this->load->view('includes/template', $data);  

 }

有关如何使用codeigniter检查this

获取最后一个ID的更多参考

答案 1 :(得分:0)

我检查了你的代码没有初始化变量

    $status = $this->input->post("status_button");
    $emp_array = $this->input->post("employee");
    $start_array = $this->input->post("start_time");
    $pid_array = $this->input->post("pid");
    $total_array = $this->input->post("total");
    if($status == "leave_open")
    {
        $button_status="open";
    }
    else
    {
        $button_status="";
    }
    // $data_to_store is not initialized and you are trying to store the value that's why it give you error
   $insert_id = $this->billing_model->store_bill($data_to_store);