【问题标题】:clears password instead of updating password/reset password清除密码而不是更新密码/重置密码
【发布时间】:2013-09-25 00:16:49
【问题描述】:

我试图允许用户更改他们的密码,但不是用新密码替换旧密码,而是删除/清除数据库中的密码字段。我只是在测试与数据库的交互,因此我还没有使用模型。

控制器

<?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 = md5($this->input->post('new_password')); 
                $fixed_password ='password';
                $this->db->where('email', $email)->update('users' ,$fixed_password );

                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();
?>

【问题讨论】:

    标签: php mysql html codeigniter


    【解决方案1】:

    看看这些行:

    $fixed_password = md5($this->input->post('new_password'));
    $fixed_password ='password';
    $this->db->where('email', $email)->update('users' ,$fixed_password );
    

    您为同一个变量分配了 2 个不同的值,其中最后一个是会粘住的值,即$fixed_password 将具有值password,无论用户输入什么,都没有其他值。其次,查看位于http://ellislab.com/codeigniter/user-guide/database/active_record.html#update 的 CodeIgniter 中 ActiveRecord 类的文档。从中您应该能够弄清楚如何正确调用update() 方法,它需要一个关联数组或一个对象作为第二个参数。我建议这次使用一个数组,因为这将是最简单的,而且这似乎就是你要去的地方。希望对您有所帮助!

    【讨论】:

    • if(md5($this->input->post('cur_password'))== $db_password){ $fixed_pa​​ssword = md5($this->input->post('new_password') );$update = $this->db->query("更新 'users' SET 'password' = '$fixed_pa​​ssword' WHERE 'email' = '$email'") or die(mysql_error()); echo "密码已更新!" ; } else { echo "密码不正确!"; }
    • 请您在这里指出正确的方向。我该如何开始为此使用数组?尝试了上面的代码,我的查询结构出现错误非常感谢提前@lfxgroove的一些帮助
    • 你看过我链接的 CodeIgniter 中update() 方法的文档吗?有一两个例子可以帮助你。您无需使用query(),因为这是非常标准的做法。
    【解决方案2】:
    $this->db->where('email', $email)->update('users' ,$fixed_password );
    

    这条线看起来不对。您似乎更新了用户名而不是密码字段。

    【讨论】:

    • 你是不是建议我把它改成 "$this->db->where('password', $db_password->('users' ,$fixed_pa​​ssword );" 因为我试过了删除数据库中的密码
    猜你喜欢
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多