Jump to content

Call to a member function where() on a non-object error help


RalphLeMouf

Recommended Posts

Hello -

 

I am fairly new to code igniter and have unfortunately hit a wall with the simple task of logging in a user to my db. I have done plenty of research on the cause of the error and it seems like it's a connection to my db issue ( which I've used troubleshooting methods on that scenario)

 

I have tried re-making the login area with a more manual approach ( no helpers or auto loads ) and still am getting the same error for my model:

 

Call to a member function where() on a non-object in /Users/my_computer/Sites/CodeIgniter/application/models/user_model.php on line 7

 

Here is my model:

 

<?php

class User_model extends CI_Model {

function validate () 
{
	$this->db->where('email', $this->input->post('email'));
	$this->db->where('password', sha1($this->input->post('password')));
	$this->db->get('users');

	if($query->num_rows == 1)
	{
		return true;
	}
}
}
?>

 

View:

 

 

<title>Login</title>

 

<!--MAKE SURE SIGNED OUT HEADER IS IMPLEMENTED FOR ALL SIGNED OUT PAGES INCLUDING THIS ONE-->

 

<div class="structure clearfix">

<h1 class="title_header">

Sign In

</h1>

<div id="signin_form">
	<?php
	echo validation_errors(); 
	echo form_open('auth/validate_credentials');
	echo "<div class='form_text_signin'>";
	echo "Email";
	echo "</div>";
	echo form_input('email');
	echo "<div class='form_text_signin'>";
	echo "Password";
	echo "</div>";
	echo form_input('password');
	echo form_submit('submit', 'Submit');
	echo form_close();
	?>
</div>
</div>

 

Controller:

 

 

<?php
class Auth extends CI_Controller {

// this is automatically called if no other function is called
// it simply turns around and calls the login() function to show the login page

public function index() {

	$this->login();
}

public function login() {	
$data['main_content'] = 'auth/login_form';
$this->load->view('includes/templates/main_page_template', $data);

}

function validate_credentials () {
	$this->load->model('user_model');
	$this->user_model->validate();

	if($query)
	{
		$data = array(
			'email' => $this->input->post('email'),
			'password' => $this->input->post('password'),
			'is_logged_in' => true
		);

		$this->session->set_userdata($data);
		redirect('account/dashboard');
	}
	else
	{
		$this->index();
	}
}
}

?>

 

Additionally here is the relevant information from the autoload.php file:

 

$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url', 'form');

 

as well as from my database.php file:

 

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'my_site_db';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

 

thanks so much in advance. :P

Link to post
Share on other sites

Yeah that's what seems to be the issue,however I have tried loading it manually and with the autohelper function and it's still posing that error. Do you happen to have another suggestion or is there something I am blatantly missing?

Link to post
Share on other sites

Yeah guy's I'm stuck. I've tried so many different things and am just clueless as to why it's STILL posing this error. For some reason it's not connecting to my db.

 

This is what I currently have for the model ( which is the page posing the error and disabling the page from running the validate_credentials function

 

<?php

class User_model extends CI_Model {

public function __construct() 
     {
           parent::__construct(); 
           $this->load->database();
     }


function validate () 
{	$this->db->select('*');
	$this->db->from('users');
	$this->db->where('email', $this->input->post('email'));
	$this->db->where('password', sha1($this->input->post('password')));
	$validate_user = $this->db->get();

	if($validate_user->num_rows == 1) {
	return TRUE;
	}
  }
}

Link to post
Share on other sites

Honestly I think this should go in third party since it's Code Igniter.

 

Since I don't know CI, I can only guess.

 

You need to look in the code for CI_Model and see how the database gets loaded. Something is not working there. Your code is expecting db to be an object and it's not, so you need to find the code that creates that $db variable.

Link to post
Share on other sites

You are not passing the params to the function.  This is my login controller method + model

 

controllers/account

<?php
// --------------------------------------------------------------------
function login()
{
// Check to see if the user is logged in
if ($this->user->logged_in())
{
	// User is logged in, redirect them
	redirect('account');
}

// Retrieve login submit from account/login
$login = $this->input->post('login');

// Login comes from navbar
if( !$login )
{		
// Retrieve the forms
$redirect = $this->input->post('redirect');

// Set form validation rules
$this->form_validation->set_rules('login_email', 'Email', 'trim|required|callback__check_login');
$this->form_validation->set_rules('login_pass', 'Password', 'trim|required');

// Check for timeout
if( $this->session->userdata('timeout') )
	$this->form_validation->set_rules('captcha', 'Image Verification', 'trim|required|callback__check_captcha');

// Form validation passed, so continue
if (!$this->form_validation->run() == FALSE)
{
	// Login the user
	if(!$this->user->login($this->input->post('login_email'), $this->input->post('login_pass'), $this->input->post('remember')))
	{
		// Login failed, alert the user
		$this->form_validation->set_message('login_failed', 'Invalid credentials');
	}
	else
	{
		// Redirect the user
		redirect($redirect);
	}
}

// Load the login view
$this->load->view(SITE . 'login');
}

 

models/user_model

<?php
// --------------------------------------------------------------------
function login($email = '', $password = '', $remember = '')
{
// Check to see if we have valid data
if($email == '' OR $password == '')
{
	// Data is invalid, return FALSE
	return FALSE;
}

// Create the password
$password =  ** encryption method

// Setup the data
$data = array(
	'email' 		=> $email,
	'password' 	=> $password,
	'user_activation' 	=> '1'
);

// Retrieve the user
$user = $this->users->get_user($data);

// Check if query exists
if($user)
{
	// Check if the user wants to be remembered
	if(isset($remember))
	{
		// Remember the user
		set_cookie('email', $this->encrypt->sha1($this->input->post('login_email')), '31536000');
		set_cookie('password', $password, '31536000');
		set_cookie('username', $user->username, '31536000');
	}

	// User exists, create the session data, return TRUE
	$this->session->set_userdata('user_id', $user->user_id);	
	$this->session->set_userdata('username', $user->username);
	$this->session->set_userdata('email', $user->email);

	// Clear timeout, if it exsits
	$this->session->unset_userdata('timeout');

	return TRUE;
}
else
{
	// User doesn't exist, return FALSE
	return FALSE;
}
}

Link to post
Share on other sites

First off...thanks so much for the help

 

I tried to adapt your model and controller to mine and am getting this error:

 

Call to a member function logged_in() on a non-object in /Users/my_computer/Sites/CodeIgniter/application/controllers/auth.php on line 16

 

Here is my controller post adaptation:

 

 

<?php
class Auth extends CI_Controller {

// this is automatically called if no other function is called
// it simply turns around and calls the login() function to show the login page

public function index() {

	$this->login();
}


function login()
{
	// Check to see if the user is logged in
	if ($this->user->logged_in())
	{
		// User is logged in, redirect them
		redirect('dashboard');
	}

	// Retrieve login submit from account/login
	$login = $this->input->post('login');

	// Login comes from navbar
	if( !$login )
	{		
	// Retrieve the forms
	$redirect = $this->input->post('redirect');

	// Set form validation rules
	$this->form_validation->set_rules('email', 'Email', 'trim|required|callback__check_login');
	$this->form_validation->set_rules('password', 'Password', 'trim|required');

	// Check for timeout
	if( $this->session->userdata('timeout') )
		$this->form_validation->set_rules('captcha', 'Image Verification', 'trim|required|callback__check_captcha');

	// Form validation passed, so continue
	if (!$this->form_validation->run() == FALSE)
	{
		// Login the user
		if(!$this->user->login($this->input->post('email'), $this->input->post('password'), $this->input->post('remember')))
		{
			// Login failed, alert the user
			$this->form_validation->set_message('login_failed', 'Invalid credentials');
		}
		else
		{
			// Redirect the user
			redirect($redirect);
		}
	}

	// Load the login view
	$this->load->view('auth/login');
}

function __construct()
        {
            parent::__construct();
            $this->load->model('user_model');
        }


}
  }




Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.