我想更改登录用户的密码,但它总是给我一个错误:[消息:试图在控制器中获取非对象的属性"密码"]。如何解决此错误??? 请帮忙,非常感谢!
这是我的控制器:
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);
}
}
谢谢!
你的语法错误说你正在尝试获取non-object
的属性$passwd
这意味着它可能是一个数组
if($passwd['password'] == $cur_password)
如果你有空user_id
将这两行放在if($this->form_validation->run()){
行上方的控制器函数中
$userid = $this->queries->login()->id; //id of the user which is currently logged IN
$this->session->set_userdata('user_id', $userid);
并在模型中的登录功能
function login() {
$query = $this->db->select('*')
->from('users')
->where('username',$this->session->userdata('username'));
return $query->row();
}
希望对您有所帮助!
当找不到user_id
时,会发生这种情况。
请注意,如果找到用户(检查是否num_rows > 0
(,getCurPassword
函数将返回用户,但如果未找到,则返回NULL
。
发生这种情况时,$passwd
var 为空,因此您无法访问$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();
}
}
请注意,我假设您的数据库中有"用户名"列