2016-11-27 137 views
3

我對PHP很陌生,沒有出現錯誤但出現錯誤,無法更新數據庫中的數據。無法在codeigniter中更新我的數據庫記錄,沒有錯誤出現

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

'echo $ this-> db-> last_query();'更新後,看看有什麼問題 –

+0

@FastSnail什麼也沒有發生。 – jrpf

+0

什麼都沒有?你應該看到實際的mysql查詢 –

回答

1

只是做

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

從模型的更新方法刪除$this->db->set($data);。這不是必需的,因爲更新查詢數據部分用於設置表記錄。

相關問題