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

标签 php mysql codeigniter sql-update

#表名为users#

## 型号名称为 user_model##

### Controller 名称是 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!";
  }

}

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

最佳答案

Controller (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;
}

查看(用户/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

关于php - 需要更新 codeigniter 上的用户表密码字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45150648/

相关文章:

Mysql:为什么我无法向数据库添加字段?

mysql - 租赁列表不再附加到 MySQL 中的用户 ID

php - Laravel 5 在尝试在 View 中输出时如何处理其中包含空格的数据库列名

php - 如何在php codeIgniter中更新相同的字段但不同的表

database - CodeIgniter手动数据库连接问题-用法

php - Paypal 成功网址无效 : Codeigniter

php - Mysql INSERT SET类型

php - 在 prestashop 中手动更新产品数量(外部脚本)

php - 具有多个结果错误的mysql子查询

php - Yii crud更新错误400