php - 删除密码而不是重置密码

标签 php mysql codeigniter wamp

大家早上好,请有人帮助我,我有一个我无法理解的小错误。我想更新/更改密码,其中用户电子邮件 = 例如 123@gmail。但是,当我输入新密码时,它会删除旧密码并将该字段留空。

  **Controller**
       <?php
 class My_account extends CI_Controller{
public function index(){
    $this->load->view("vChangePassword");
}

public function change_password()
{

    $this->load->library('form_validation');

    $this->form_validation->set_rules('cur_password','Current Password','required');
   $this->form_validation->set_rules('new_password','New Password','required');
   $this->form_validation->set_rules('con_password','Confirm Password', 'required[matches[new_password]');
    if ($this->form_validation->run()!= true){

        $this->load->view("vChangePassword");
    }else {
        $sql = $this->db->select("*")->from('users')->where('email',$this->session->userdata('email') )->get();
    foreach($sql->result() as $my_info)
    {
        $db_password = $my_info->password;
        $email = $my_info->email; 
    }
      if(md5($this->input->post('cur_password'))== $db_password){
        $fixed_password = mysql_real_escape_string(md5($this->input->post('new_password')));
       // $update = $this->db->query("Update 'users' SET 'password' = '$fixed_password' WHERE 'id' = '$db_id'") or die(mysql_error());
       $fixed_password ='password';
      // $fixed_password = md5($this->input->post('new_password'));
       $this->db->where('email', $email)->update('users' ,$fixed_password );
      // $update = $this->db->query("Update 'users' SET 'password' = '$fixed_password' WHERE 'id'= '$db_id'")or die(mysql_error());
      echo "Password has been updated!" ;  

      }
      else {
          echo "Password is incorrect!";
      }
    }


}
}


?>

查看

<?php
 echo form_open(base_url()."index.php/my_account/change_password")?>
 <?php echo validation_errors();
   ?>
 <table class=”table table-bordered”>
  <tbody>
  <tr>
<td><small><?php echo 'Old Password:';?></small></td>
<td><?php echo form_password("cur_password");?></td>
</tr>
<tr>
<td><small><?php echo 'New Password:';?></small></td>
<td><?php echo form_password("new_password");?></td>
</tr>
  <tr>
  <td><small><?php echo 'Confirm Password:';?></small></td>
  <td><?php echo form_password("con_password");?></td>
  </tr>
  </tbody>
   </table>
 &nbsp;&nbsp;<div id=”some”style=”position:relative;”><button type=”submit” class=”btn    btn-primary”><i class=” icon-ok-sign icon-white”></i>&nbsp;Submit</button>
<?php
echo form_close();
 ?>

编辑表结构

CREATE TABLE IF NOT EXISTS users (
    id INT (11) NOT NULL AUTO_INCREMENT,
    email VARCHAR (255) NOT NULL,
    `password` VARCHAR (255) NOT NULL,
    `status` VARCHAR (255) NOT NULL,
    `Level` INT (11) NOT NULL,
    username VARCHAR (255) NOT NULL,
     avatar TEXT NOT NULL,
     signup_date INT (10) NOT NULL,
     PRIMARY KEY (id) >
  ) ENGINE = INNODB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 9 ;

最佳答案

问题是您没有制作正确的数组来更新密码,而是将生成的 md5 字符串更新为

$fixed_password ='password'; wrong

试试这个

$fixed_password = mysql_real_escape_string(md5($this->input->post('new_password')));
$data = array('password' => $fixed_password);

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

See Active Record Reference

关于php - 删除密码而不是重置密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18999293/

相关文章:

php - 数据库最常见条目查询

mysql - 高效在数据库中添加好友逻辑

php - 如何在 codeigniter 中正确地为表起别名?

javascript - CodeIgniter - 如何将值从 View 传递到 Controller ?

php - Symfony3 无法创建对象

javascript - 语法错误。无法在 php 文件中嵌入 html 代码和 javascript 操作

php - php/mariadb 更新后禁止加载数据本地文件

php - 经常更新大型 MySQL 数据库

MySQL' 未被识别为内部或外部命令 - VS2015 中的 Cordova

php - 升级后 Codeigniter 查询生成器对象错误