簡體   English   中英

獲取最后插入的id codeigniter並在視圖中顯示

[英]Get the last inserted id codeigniter and display in view

大家好,我想獲取最后插入的ID並將其顯示在我的視圖中。

以下是我的代碼,

控制器:

    $this->load->view('bootstrap/header');
    $this->load->model('CustomersModel');
    $data['query'] = $this->CustomersModel->viewallcustomers(); 
    $data['last_id'] = $this->db->insert_id();

    $this->load->library('form_validation');
    $this->form_validation->set_rules(array(
        array(
            'field' => 'c_fname',
            'label' => 'Customer Firstname',
            'rules' => 'required'
            ),
        array(
            'field' => 'c_lname',
            'label' => 'Customer Lastname',
            'rules' => 'required'
            )
    ));

    $this->form_validation->set_error_delimiters('<div class="alert alert-error">', '</div>');
    if(!$this->form_validation->run()) {
        $this->load->view('customer_form', $data);
    }

模型:

public function viewallcustomers()
{

    $this->db->select('*');
    $this->db->from('customers');
    #$this->db->join('customers', 'salesmonitoring.customer_id = customers.customer_id');
    #$this->db->order_by("salesmonitoring.sales_id", "desc"); 

    $query = $this->db->get();

    return $query->result();

}

視圖:

         <div class="control-group">
        <?php 
         $form_label_attributes = array('class' => 'control-label');
        echo form_label('Customer ID', 'customer_id', $form_label_attributes); ?>
        <div class="controls">
            <?php echo form_input(array('name' => 'customer_id', 'value' => $last_id, 'readonly' => 'true') ); ?>
        </div>
    </div>

但是它只顯示0。我的代碼有什么問題? 非常感謝您的幫助。 謝謝。

$this->db->insert_id(); 在插入代碼之后使用,然后返回最后插入的ID。

您可以使用$this->CustomersModel->db->select_max("id")->get('tableName'); 而是$this->db->insert_id();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM