2014-02-26 132 views
0

我正在尝试更新codeigniter中的特定行表。但我的代码更新所有行。使用codeigniter更新特定行?

型号:

function edit_profile($array){ 
     $email=$array['email']; 
     $this->db->select("email"); 
     $this->db->from("user"); 
     $this->db->where('email', $email); 
     $query=$this->db->get(); 
     if ($query->num_rows() == 1){ 
     $this->db->update('user',$array); 
      return 1; 

     }else{ 
      return $query->num_rows(); 
     } 
    } 

控制器:

 public function edit() 
     { 
     $name = $this->input->post('user_name', TRUE); 
     $email=trim($this->session->userdata('email')); 
     $array = array('user_name'=>$name,'email'=>$email); 
     $result = $this->edit_profile_model->edit_profile($array); 
     echo $result; 
     } 

而且在新的页面显示它的回声值1 如何更新特定行?为什么$query->num_rows()返回1并更新所有行?

+0

什么是你想更新?用户名或电子邮件? –

+0

@Nouphal用户名。 – mridul

回答

3

尝试使用where状态更新特定的记录

$updateData = array(
    'user_name'=>$array['user_name']; 
); 
$this->db->where('email', $array['email']); 
$this->db->update('user', $updateData); 
1
function edit_profile($username, $email){ 
    $this->db->set('username',$username); 
    $this->db->where('email', $email); 
    $this->db->update('user'); 
    $result = $this->db->affected_rows(); 
    return $result 
    } 

控制器

public function edit() 
    { 
    $name = $this->input->post('user_name', TRUE); 
    $email=trim($this->session->userdata('email')); 
    $result = $this->edit_profile_model->edit_profile($name,$email); 
    echo $result; 
    } 
0

在模型中只使用

$update_data = array('coulmn1_name'=>'value','coulmn2_name'=>'value'); 

$this->db->where('email', $email); 
$this->db->update('my_table', $update_data);