1

I want to change the password for logged in users,but it always give me an error:[Message: Trying to get property 'password' of non-object] in the controller. How to solve this error??? Please help,Thanks much!

This is my controller:

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

                }

This is my model:

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

}

Thanks!

Anuvrat Parashar
  • 2,960
  • 5
  • 28
  • 55
jovan2226
  • 17
  • 5
  • could you please post the full error message especially the part which mentions where in your code the error was raised? – Anuvrat Parashar Oct 02 '18 at 07:50
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Nico Haase Oct 02 '18 at 10:13
  • @jovan2226 Did you succeed solving the issue? – dWinder Oct 02 '18 at 12:56
  • @David Winder i got an error:[Unknown column 'user_name' in 'where clause' SELECT * FROM `users` WHERE `user_name` = 'admin'] – jovan2226 Oct 03 '18 at 00:57
  • @jovan2226 - As I said, I assume your DB has column name `user_name`, what is the scheme of your `users` table? do you have a user name "admin"? please respond in the chat-link given by @Samuel in my answer comments – dWinder Oct 03 '18 at 03:54
  • Please check my updated answer @jovan2226 – Leena Patel Oct 03 '18 at 09:30

2 Answers2

1

This happens when the user_id not found.

Notice that the getCurPassword function will return the user if found (when checking if num_rows > 0) but if didn't found it returns NULL.

When this happens the $passwd var is null so you cann't access $passwd->password.

You can solve it by changing the if statement to:

if($passwd && $passwd->password == $cur_password){

Edited Try to retrieve your user name as and then call with it the getCurPassword function:

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

And in the controller change getCurPassword function as:

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

Notice that I assume you have "username" column in your DB

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • @jovan2226 sorry for the late answer. I think I got syntax error in the query of `getCurPassword` - I edited the answer. If it is still relevant you can check again – dWinder Oct 20 '18 at 16:05
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!

Leena Patel
  • 2,423
  • 1
  • 14
  • 28