无法在codeigniter中更新我的数据库记录,不会出现错误

时间:2016-11-27 09:55:09

标签: php codeigniter codeigniter-3

我对php很陌生,并且没有出现任何错误,但却无法更新数据库中的数据。

The controller

  public function update_user_view() { 
     $this->load->helper('form'); 
     $user_id = $this->uri->segment('3'); 
     $query = $this->db->get_where("users",array("user_id"=>$user_id));
     $data['records'] = $query->result(); 
     $data['old_user_id'] = $user_id; 
     $this->load->view('user_edit',$data); 
  } 

  public function update_user(){ 
     $this->load->model('user_model');

     $data = array(  
        'user_id' => $this->input->post('user_id'),
        'name' => $this->input->post('name'), 
        'nickname' => $this->input->post('nickname'), 
        'email' => $this->input->post('email'), 
        'hadd' => $this->input->post('hadd'), 
        'gender' => $this->input->post('gender'), 
        'cpnum' => $this->input->post('cpnum'), 
        'comment' => $this->input->post('comment') 

     ); 

     $old_user_id = $this->input->post('old_user_id'); 
     $this->user_model->update($data,$old_user_id); 

     $query = $this->db->get("users"); 
     $data['records'] = $query->result(); 
     $this->load->view('user_view',$data); 
  } 

the model

<?php 
   class User_model extends CI_Model {

      function __construct() { 
         parent::__construct(); 
      } 

      public function insert($data) { 
         if ($this->db->insert("users", $data)) { 
            return true; 
         } 
      } 

      public function delete($user_id) { 
         if ($this->db->delete("users", "user_id = ".$user_id)) { 
            return true; 
         } 
      } 

      public function update($data,$old_user_id) { 
         $this->db->set($data); 
         $this->db->where("user_id", $old_user_id); 
         $this->db->update("users", $data); 
      } 
   } 
?> 

2 个答案:

答案 0 :(得分:1)

只做

$this->db->where("user_id", $old_user_id); 
$this->db->update("users",$data);

答案 1 :(得分:0)

从模型的更新方法中删除$this->db->set($data);。这不是必需的,因为更新查询数据部分用于设置表记录。