更新表中的所有记录而不是单个记录

时间:2013-09-05 09:29:34

标签: codeigniter

我是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;  
        } 


    }

我的代码中有什么问题,请帮忙。

2 个答案:

答案 0 :(得分: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;  
    } 


}

答案 1 :(得分:1)

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

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