-1

I hope somebody will explain or help me to find out what the problem is. I have such an error while trying to register.

Message: Undefined property: Pages::$user_model. Call to a member function email_check() on null.

Also, It says that I should check line 45. Here is this line and line above:

$this->load->model('User_model');
$email_check=$this->user_model->email_check($user['email']);

So, this is my controller. I use database "mysite", table "mysite".

class Pages extends CI_Controller {
function view($page ='home'){

    $this->load->helper('url');
$this->load->model('User_model');
$this->load->library('session');
    $data['title'] = $page;
    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page);
    $this->load->view('templates/Navigation', $data);
    $this->load->view('templates/footer', $data);
    }

public function __construct()
   {
    parent::__construct();
    $this->load->helper('url');
$this->load->model('User_model');
$this->load->library('session');
   }

public function index()
 {
    $this->load->helper('url');
    $this->load->model('User_model');
    $this->load->library('session');
    $this->load->view("Register.php");
}

public function register_user(){
    $this->load->helper('url');
    $this->load->model('User_model');
    $this->load->library('session');
$user=array(
  'username'=>$this->input->post('username'),
  'email'=>$this->input->post('email'),
  'password'=>md5($this->input->post('password')),
  'age'=>$this->input->post('age'),
  'phone'=>$this->input->post('phone')
    );
print_r($user);
    $this->load->model('User_model');
    $email_check=$this->user_model->email_check($user['email']);
    if($email_check){
          $this->load->model('User_model');
        $this->user_model->register_user($user);
        $this->session->set_flashdata('success_msg', 'Registered successfully.Now login to your account.');
        redirect('pages/login_view');
        }
    else{
        $this->load->model('User_model');
    $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
    redirect('pages');
    }
}

public function login_view(){
    $this->load->view("pages/Login");
}

function login_user(){
    $this->load->model('User_model');
$user_login=array(
    'email'=>$this->input->post('email'),
    'password'=>md5($this->input->post('password')));
        $this->load->model('User_model');
  $data=$this->User_model->login_user($user_login['email'],$user_login['password']);
if($data)
  {
    $this->session->set_userdata('user_id',$data['user_id']);
    $this->session->set_userdata('email',$data['email']);
    $this->session->set_userdata('username',$data['username']);
    $this->session->set_userdata('age',$data['age']);
    $this->session->set_userdata('phone',$data['phone']);
    $this->load->view('pages/User_profile');
  }
  else{
            $this->load->model('User_model');
    $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
    $this->load->view("pages/Login");
  }
}

function user_profile(){
    $this->load->model('User_model');
        $this->load->view('user_profile.php');
}

public function user_logout(){
    $this->load->model('User_model');
$this->session->sess_destroy();
redirect('pages/login_view', 'refresh');
}
}
?>

And this is my User_model. Actually, i have no idea where problem is.

    <?php
class User_model extends CI_model{
        public function register_user($user){
                $this->db->insert('mysite', $user);
            }
public function login_user($email,$password){
    $this->db->select('*');
    $this->db->from('mysite');
    $this->db->where('email',$email);
    $this->db->where('password',$password);

    if($query=$this->db->get())
    {
    return $query->row_array();
    }
    else{
        return false;
    }
    }

    public function email_check($email){
    $this->db->select('*');
    $this->db->from('mysite');
    $this->db->where('email',$email);
    $query=$this->db->get();

    if($query->num_rows()>0){
        return false;
    }else{
    return true;
}
}
}
?>
tereško
  • 58,060
  • 25
  • 98
  • 150
mumba
  • 1
  • 1
  • Are you sure in `$user['email']` the data is there? – Shihas Mar 11 '18 at 12:18
  • I am not sure but I think it occurs when you are trying to register with an existing email address. If it is, then you can check email address if it is unique or not, to your controller. – Mustafa Mar 11 '18 at 13:40
  • CI file/class names are case sensitive (ucfirst() rule should be used). Try with `$email_check=$this->User_model->email_check($user['email']);`. – Tpojka Mar 11 '18 at 15:18
  • @Tpojka, just tried, and didnt help :( – mumba Mar 11 '18 at 17:01
  • @Shihas, i this everything is ok with data, as i got it: Array ( [username] => roland625 [email] => helo@gmail.com [password] => 827ccb0eea8a706c4c34a16891f84e7b [age] => 22 [phone] => 4567890 ) – mumba Mar 11 '18 at 17:02
  • @mumba Error from question means something with loading of model is not ok, since application can't recognise property which should be model instance (in case Ucfirst rule was followed). You are trying to load model multiple times. Leave `$this->load->model('User_model');` in `__construct()` method only and remove that line from other controller's methods. Also for convenience have `__construct()` method as first method in controller. Give a shot. – Tpojka Mar 11 '18 at 17:46
  • @Tpojka, made the same, nothing :( – mumba Mar 11 '18 at 18:12
  • I am trying with coding standards, remove php closing tags. Also tell which URL you are calling here when error is shown. – Tpojka Mar 11 '18 at 21:07
  • 1
    check the sensitive case sir, may you have the error from there. User_model vs user_model. – Gilang Pratama Mar 11 '18 at 22:18

1 Answers1

0

You can try this:` In controller:

$this->load->model('User_model');
$email_check=$this->user_model->email_check($user);

and in model

public function email_check($user=array()){
    $this->db->select('*');
    $this->db->from('mysite');
    $this->db->where('email',$user['email']);
    $query=$this->db->get();

    if($query->num_rows()>0){
        return false;
    }else{
    return true;
}
}
user10
  • 1
  • 1