【问题标题】:Update specific row using codeigniter?使用 codeigniter 更新特定行?
【发布时间】:2014-02-26 07:28:57
【问题描述】:

我正在尝试更新 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 并更新所有行?

【问题讨论】:

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

标签: php mysql codeigniter


【解决方案1】:

尝试使用where 条件更新特定记录

$updateData = [
   'user_name' => $array['user_name'],
];
$this->db->where('email', $array['email']);
$this->db->update('user', $updateData); 

【讨论】:

    【解决方案2】:
        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;
        }
    

    【讨论】:

      【解决方案3】:

      在您的模型中只需使用

      $update_data = array('coulmn1_name'=>'value','coulmn2_name'=>'value');
      
      $this->db->where('email', $email);
      $this->db->update('my_table', $update_data);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-15
        • 2011-12-30
        • 1970-01-01
        相关资源
        最近更新 更多