-1

我是 codeigniter 的新手,并且正在从事一个我必须更新用户记录的 codeignter 项目。传递给模型的用户 id 和数据是正确的,但是当我更新记录时,所有用户都更新为新记录。我的编码是:

public function updateUser(){
    $user = $this->session->userdata('logged_in');
    $userID = $user['id'];
    $data = array(
                                "first_name"        =>      $this->input->post('reg_first_name'),
                                "last_name"     =>      $this->input->post('reg_last_name'),
                                "mobile"            =>      $this->input->post('reg_mobile'),
                                "country"   =>      $this->input->post('reg_country'),
                                "state"     =>      $this->input->post('reg_state'),
                                "city"      =>      $this->input->post('reg_city'),
                                "paypal_email"  =>      ''
                        );

    $this->db->update('users',$data);
    $this->db->where('id',$userID);
    if($this->db->affected_rows() > 0){

            return true;    
        }
        else {
            return false;  
        } 


    }

我的代码有问题吗,请帮忙。

4

2 回答 2

2

尝试 :

public function updateUser(){
$user = $this->session->userdata('logged_in');
$userID = $user['id'];
$data = array(
                            "first_name"        =>      $this->input->post('reg_first_name'),
                            "last_name"     =>      $this->input->post('reg_last_name'),
                            "mobile"            =>      $this->input->post('reg_mobile'),
                            "country"   =>      $this->input->post('reg_country'),
                            "state"     =>      $this->input->post('reg_state'),
                            "city"      =>      $this->input->post('reg_city'),
                            "paypal_email"  =>      ''
                    );
$this->db->where('id',$userID);    //changes here  where clause should be provided before update
$this->db->update('users',$data);
if($this->db->affected_rows() > 0){

        return true;    
    }
    else {
        return false;  
    } 


}
于 2013-09-05T09:30:33.473 回答
1

你的看起来不错,但唯一的问题是......你首先更新你的数据库并添加它的位置......这不是更新查询之前应该是的位置。

 $this->db->where('id',$userID);
 $this->db->update('users',$data);
于 2013-09-05T09:37:47.333 回答