【问题标题】:Codeigniter:How to get the last inserted id?Codeigniter:如何获取最后插入的 id?
【发布时间】:2015-09-26 11:38:16
【问题描述】:

您好,我正在使用 codeigniter。我需要获取最后插入的 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-&gt;db-&gt;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

有人可以帮助我吗?提前致谢

【问题讨论】:

  • 什么是$data_to_store变量?
  • 这是一个数组,我从 @AnkiiGangrade 表单中获取 post 值
  • 你没有在你的代码中创建任何数组$data_to_store!!
  • 在哪里使用?你能告诉我@Saty
  • $insert_id = $this-&gt;billing_model-&gt;store_bill($data_to_store); 用于定义 $data_to_store 的这一行??

标签: mysql codeigniter


【解决方案1】:

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

<?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 的更多参考

【讨论】:

  • 我们可以在控制器中进行查询吗? @Rakesh
【解决方案2】:

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

    $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);

【讨论】:

  • 我这样做是因为我得到了上面的错误。我需要得到我最后插入的 id @Mitul
  • 您需要将表格字段更改为自动增量
  • 我不想要自动增量。我需要控制中的 id .. @Mitul
  • 是的,它发生了,但我不想要它。对于每个项目,我将它们插入我的桌子,但账单 ID 将是相同的。所以我需要控制id。你能得到我吗? @Mitul
  • 你将使用Maximum函数$maxid = $this-&gt;db-&gt;query('SELECT MAX(id) AS maxid` FROM bill')->row()->maxid;`得到最后插入的id;`
猜你喜欢
  • 2017-02-06
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
相关资源
最近更新 更多