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

阿努

嗨,我正在使用codeigniter。我需要获取最后插入的id并将其递增并在我的视图页面My controller code中使用它:

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

有人能帮我吗?提前致谢

瑞奇

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

<?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获取最后的ID,请检查此内容

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在codeigniter中插入查询后获取最后的插入ID

来自分类Dev

如何从Laravel 4获取最后的插入ID

来自分类Dev

如何使用Hibernate获取最后插入的ID

来自分类Dev

如何获取最后插入的行的ID

来自分类Dev

如何使用AJAX获取最后插入的ID?

来自分类Dev

如何获取特定ID的最后插入记录?

来自分类Dev

获取最后插入的ID

来自分类Dev

如何获取和插入ID CodeIgniter

来自分类Dev

如何从PDO获取最后插入的插入行ID

来自分类Dev

如何从插入方法Laravel 5.1获取最后插入的ID

来自分类Dev

如何在多个记录插入中获取最后插入的ID?

来自分类Dev

如何插入 ID +1 并获取最后一个 ID?

来自分类Dev

在codeigniter中,是否可以从bach插入中获取最后插入的ID?

来自分类Dev

获取Postgresql的最后插入ID

来自分类Dev

获取最后插入行的 id

来自分类Dev

异常获取最后插入的 id

来自分类Dev

如何获取插入的最后一行的id(自动递增)

来自分类Dev

如果使用实体框架,如何获取最后插入的ID

来自分类Dev

如何获取linq中的最后一个插入ID

来自分类Dev

如何从PhoneGap Android中的SQLite获取最后插入的行ID

来自分类Dev

如何在Laravel中获取最后插入的ID?

来自分类Dev

如何在雄辩的ORM laravel中获取最后的插入ID

来自分类Dev

如何在Laravel中获取最后插入的ID

来自分类Dev

Nodjs区分如何获取最后一个插入ID

来自分类Dev

如何使用C#在SQLite中获取最后插入的ID?

来自分类Dev

如何从PhoneGap Android中的SQLite获取最后插入的行ID

来自分类Dev

如何在zf2中获取最后插入的ID?

来自分类Dev

如何获取linq中的最后一个插入ID

来自分类Dev

如何获取MVC中最后一次插入的ID?

Related 相关文章

热门标签

归档