2016-11-24 101 views
-1

我無法編輯和更新第一個CodeIgniter項目中的項目。當我點擊編輯按鈕時,首先它會加載視圖,並在數據庫中填充該項目的信息,但不會加載我正在使用的引導程序。編輯和更新CodeIgniter中的項目

然後,當我點擊「編輯」按鈕,返回大量的錯誤說:

  • 我對控制器的功能是缺失的參數和變量是不確定的,而

  • 我的視圖有一個未定義的偏移量,並試圖獲取非對象的屬性。

CRUD的所有其他命令正在工作,除此之外。

我使用的笨的版本是2.2.6

代碼:

  • 型號:

    function edit($id) 
    { 
        $this->db->where('id', $id); 
        $query = $this->db->get('products'); 
        return $query->result(); 
    } 
    
    function update($data) 
    { 
        $this->db->where('id', $data['id']); 
        $this->db->set($data); 
        return $this->db->update('products'); 
    } 
    
  • 控制器:

    function edit($id) 
    { 
        $data['title'] = "Edit Product"; 
        $data['product_data'] = $this->model->edit($id); 
        $this->load->view('productEdit', $data); 
    } 
    
    function update() 
    { 
        $this->load->library('form_validation'); 
        $this->form_validation->set_error_delimiters('<span>', '</span>'); 
        $validations = array(
         array(
          'field' => 'name', 
          'label' => 'Name', 
          'rules' => 'trim|required|max_length[255]' 
         ), 
         array(
          'field' => 'price', 
          'label' => 'Price', 
          'rules' => 'trim|required|max_length[255]' 
         ), 
         array(
          'field' => 'stock_quantity', 
          'label' => 'In Stock', 
          'rules' => 'trim|required|max_length[255]' 
         ) 
        ); 
        $this->form_validation->set_rules($validations); 
        if ($this->form_validation->run() === FALSE) { 
         $this->edit($this->input->post('id')); 
        } else { 
         $data['id'] = $this->input->post('id'); 
         $data['name'] = $this->input->post('name'); 
         $data['price'] = $this->input->post('price'); 
         $data['stock_quantity'] = $this->input->post('stock_quantity'); 
         if ($this->model->update($data)) { 
          redirect('products'); 
         } else { 
          log_message('error', 'Error'); 
         } 
        } 
    } 
    
  • 查看:

    <?php echo form_open('products/edit/', 'id="form-products"'); ?> 
    
    <input type="hidden" name="id" value="<?php echo $product_data[0]->id; ?>"/> 
    
    <label for="nome">Product Name:</label><br/> 
    <input type="text" name="name" value="<?php echo $product_data[0]->name; ?>"/> 
    <div class="error"><?php echo form_error('name'); ?></div> 
    
    <label for="email">Price:</label><br/> 
    <input type="text" name="price" value="<?php echo $product_data[0]->price; ?>"/> 
    <div class="error"><?php echo form_error('price'); ?></div> 
    
    <label for="email">In Stock:</label><br/> 
    <input type="text" name="stock_quantity" value="<?php echo $product_data[0]->stock_quantity; ?>"/> 
    <div class="error"><?php echo form_error('stock_quantity'); ?></div> 
    
    <input type="submit" name="update" value="Update" /> 
    
    <?php echo form_close(); ?> 
    

謝謝您的時間。

回答

0
  • 試試這個 更新模型 function edit($id) { $this->db->where('id', $id); $query = $this->db->get('products'); return $query->row(0); }

,然後在您的視圖。

<input type="hidden" name="id" value="<?php echo $product_data->id; ?>"/> 

也改變你的表單操作

products/update/