简体   繁体   中英

Can't update my database record in codeigniter, no error is appearing

I'm quite new to php and there's no error appearing but appereantly, can't update my data in the database.

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); 
      } 
   } 
?> 

Just do

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

Remove $this->db->set($data); from update method of model. This is not required as update query data section is used to set table records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM