Php 在codeigniter中更改用户密码的简单代码

Php 在codeigniter中更改用户密码的简单代码,php,codeigniter,Php,Codeigniter,我需要一个非常简单的代码来更改登录用户的密码。我使用了以下代码: 控制器-my_account.php public function change_password(){ $this->page_handler->consumer_page(); $this->form_validation->set_rules('opassword','Old Password','required|trim|xss_clean|callback_change');

我需要一个非常简单的代码来更改登录用户的密码。我使用了以下代码: 控制器-my_account.php

public function change_password(){
    $this->page_handler->consumer_page();
    $this->form_validation->set_rules('opassword','Old Password','required|trim|xss_clean|callback_change');
    $this->form_validation->set_rules('npassword','New Password','required|min_length[6]|max_length[32]');
    $this->form_validation->set_rules('cpassword','Confirm Password','required|matches[npassword]');
    if($this->form_validation->run()!=true) {
        $this->load->view("passwordchange");
    }
    else {
        $sql = $this->db->select("*")->from("consumer")->where("login",  $this->session->userdata("login"))->get();

        foreach ($sql->result()as $info) {
            $db_password = $info ->password;
            $login = $info ->login;
        }

        if(md5($this->input->post("opassword"))==$db_password) {
            $fixed_pw = md5(mysql_real_escape_string($this->input->post("npassword")));
            $update = $this->db->query("UPDATE 'consumer' SET 'password' = '$fixed_pw' where 'login' ='$login'") or die(mysql_error());
            $this->session->set_flashdata("notification","Password has been updated!");
            redirect("MyPage","refresh");
        }
        else {
            echo "Password is incorrect!";    
            $this->load->view("passwordchange");
        }
    }
}
模型-page_handler.php

Class Page_handler extends CI_Model{
    public function isLoggedIn(){
        return $this->session->userdata("Logged_in");
    }

    public function consumer_page(){
        if($this->isLoggedIn()) {
            return TRUE;
        }
    }
}
我收到错误消息

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: db_password
Filename: controllers/my_account.php
Line Number: 23
密码不正确!
哪里可能出错?

最好在控制器中定义两种方法

第一:

function check_old_passwd($pass){
//for set custom validation message
$this->form_validation->set_message('check_old_passwd', 'your custom message');
//{check in db...}
       if(true){
       return true;
       }
return false
}
第二:

function is_newpasswd_correct($newpass){
//for set custom validation message
$this->form_validation->set_message('is_newpasswd_correct', 'your custom message');
//check if new password is in your site terms
//not smaller than 4 digits etc.
if(true){
return true;
}
return false;
}
然后在验证规则中使用回调:

$this->form_validation->set_rules('oldpass', 'Old Password', 'required|trim|callback_check_old_passwd');

$this->form_validation->set_rules('newpass', 'New Password', 'required|trim|callback_is_newpasswd_correct');

希望它能帮助你

$db\u password=$info->password存在问题。。。改写成<代码>$this->db_password=$info->password
在变量之前使用
$this
关键字。您的查询是否返回任何结果?@Lefters,这是另一个问题,因为他在列名周围有单引号。很抱歉,这没有帮助:(查询不返回任何结果,但只显示错误。)