使用登录的用户更改codeigniter中的密码会引发错误

时间:2018-10-02 07:41:50

标签: php codeigniter codeigniter-3

我想更改登录用户的密码,但它总是给我一个错误:[消息:试图获取非对象的属性'password']。如何解决这个错误? 请帮忙,非常感谢!

这是我的控制者:

                    if($this->form_validation->run()){
                    $cur_password = md5($this->input->post('cpassword')); // md5-encrypt the password
                    $new_password = md5($this->input->post('npassword'));
                    $conf_password = md5($this->input->post('copassword'));
                    //$this->load->model('queries');
                    $this->load->library('session');
                    $user_id = $this->session->userdata('user_id');//multiple users id login

                    $passwd = $this->queries->getCurPassword($user_id);
 !ERROR FOUND HERE!->if($passwd->password == $cur_password){
                        if($new_password == $conf_password){
                            if($this->queries->updatePassword($new_password,$user_id)){
                                echo 'Password Updated Successfully!';
                                echo '<br/><label><a href="'.base_url().'main/enter">Back to homepage</a></label><br/>';
                            }else{
                                echo 'Failed To Update Password!!';
                            }
                        }else{
                            echo 'New Password and Confirm Password is not matching!!';
                        }

                    }else{
                        echo 'Sorry! Current Password is not matching!!';
                    }
                }else{
                    echo validation_errors();

                }

这是我的模特

<?php
class Queries extends CI_Model{

function login() {
    $query = $this->db->select('*')
                      ->from('users')
                      ->where('id');
    return $query->row();
}

function index() {
    $userid = $this->login()->id; //id of the user which is currently logged IN
    $this->session->set_userdata('user_id', $user_id);
}
    public function getCurPassword($user_id){   
        $query = $this->db->where(['id'=>$user_id])
                          ->get('users');
        if($query->num_rows() > 0 ){
            return $query->row();
        }
    }
    public function updatePassword($new_password,$user_id){
        $data = array(
                      //'password' => md5($this->input->post("$new_password"))
                      'password' => ($new_password)
                     //'password'=> password_hash(["$new_password"],PASSWORD_BCRYPT)
                      );
        return $this->db->where('id',$user_id)
        ->update('users',$data);
    }

}

谢谢!

2 个答案:

答案 0 :(得分:1)

当找不到user_id时会发生这种情况。

请注意,getCurPassword函数将在找到用户时(检查num_rows > 0时)返回用户,但如果未找到,它将返回NULL

发生这种情况时,$passwd变量为null,因此您将无法访问$passwd->password

您可以通过将if语句更改为:

来解决该问题。
if($passwd && $passwd->password == $cur_password){

已编辑:尝试检索您的用户名,然后使用其调用getCurPassword函数:

$user_name = $this->session->userdata('username');
$passwd = $this->queries->getCurPassword($user_name );

在控制器中,更改getCurPassword的功能为:

public function getCurPassword($user_name){   
    $query = $this->db->select('*')
              ->from('users')
              ->where('username', $user_name);
    if($query->num_rows() > 0 ){
        return $query->row();
    }
}

请注意,我假设您的数据库中有“用户名”列

答案 1 :(得分:1)

You have error in your syntax error says that you are trying to get property of non-object means $passwd may be an array

if($passwd['password'] == $cur_password)

And in case you have null user_id

Place these two lines in your controller function above if($this->form_validation->run()){ line

$userid = $this->queries->login()->id; //id of the user which is currently logged IN
$this->session->set_userdata('user_id', $userid);

and in your login function in model

function login() {
$query = $this->db->select('*')
                  ->from('users')
                  ->where('username',$this->session->userdata('username'));
return $query->row();
}

Hope it helps!