需要更新codeigniter上的用户表密码字段

时间:2017-07-17 17:53:33

标签: php mysql codeigniter sql-update

#table name是用户#

##型号名称为user_model ##

###控制器名称为get_password ###

问题 - 密码没有变化,保持旧状态

> model(user_model)



 public function updatePassword($email,$data)

 {   
      $data1=array('password'=>$data);

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

      $this->db->update('users','password'); 

    $success = $this->db->affected_rows(); 

    if(!$success){
        error_log('Unable to updatePassword');
        return false;
    }        
    return true;
} 


 > controller(get_password)

public function index($rs=FALSE)
{
$this->load->database();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->library('session');

  $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
  $this->form_validation->set_rules('password', 'password', 'required');
  $this->form_validation->set_rules('passconf', 'password Confirmation', 'required|matches[password]');

 if ($this->form_validation->run() == FALSE)
  {
              echo form_open();
              $this->load->view('users/fpre');
 }
else
 {


  $data = array(
        'password' => md5($this->input->post('password')),


  );

    $email =array(

        'email' =>$this->input->post('email')
  );



   $this->user_model->updatePassword($data,$email);

  echo "Congratulations!";
  }

}

它显示没有错误,但密码未更新在用户表中保持不变..我无法找到问题,请帮我找出来..

3 个答案:

答案 0 :(得分:1)

您的模型功能显示参数应该是电子邮件,然后是密码,但您的控制器正在通过其他方式传递它们。

$this->user_model->updatePassword($data,$email);

应该是:

$this->user_model->updatePassword($email,$data);

我也相信数据需要以不同方式传递。 where()函数需要where(field_name, value)where(array(field_name => value))。看看你的代码,你似乎混合了这两个代码。

使用set()也可以帮助解决这个问题,而不是

$data1=array('password'=>$data);
$this->db->where('email','$email');
$this->db->update('users','password');

使用:

$this->db->set($data);
$this->db->where($email);
$this->db->update('users');

注意:代码未经测试。

答案 1 :(得分:1)

控制器(get_password):

public function index() {
        $this->load->database();
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('form_validation', 'session'));
        $this->load->model('user_model');

        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Current password', 'required');
        $this->form_validation->set_rules('newpassword', 'password', 'required');
        $this->form_validation->set_rules('newpassconf', 'password Confirmation', 'required|matches[newpassword]');

        $email = $this->input->post('email');

        $success = false;
        $msg = '';

        if ($this->form_validation->run() !== FALSE) {
            $password = md5($this->input->post('password'));

            if ($this->user_model->checkPassword($email, $password)){
                $newpassword = md5($this->input->post('newpassword'));

                if ($this->user_model->updatePassword($email, $newpassword)){
                    $success = true;                    
                }else{
                    $msg = 'Unable to updatePassword';
                }               
            }else{
                $msg = 'Incorrect password';
            }
        }   

        if ($success){
            echo 'Congratulations!';        
        }else{          
            $this->load->view('users/fpre', array(
                'email'=>$email,
                'msg'=>$msg
            )); 
        }
    }

模型(user_model):

public function checkPassword($email, $password) {
    $users = $this->db->get_where('users', array('email'=>$email))->row();

    return $users->password === $password;
}

public function updatePassword($email, $newpassword) {      
    $data = array('password' => $newpassword);

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

    $success = $this->db->affected_rows();  

    if (!$success) {
        error_log('Unable to updatePassword');
    }   

    return $success;
}

查看(users / fpre):

if ($msg){
    echo 'Message: '.$msg;  
}

echo form_open();
echo form_input('email', $email);
echo form_password('password');
echo form_password('newpassword');
echo form_password('newpassconf');
echo form_submit('', 'Enviar');
echo form_close();

比较的变化: enter image description here enter image description here

答案 2 :(得分:0)

我认为此行$this->db->update('users','password');应为$this->db->update('users', $data);

目前您没有将密码传递给更新功能。您正在传递字符串“password”。