1

I am working on a login form using code igniter. I can get the form to display alright but when I click to login I get the following error displayed:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_DB_mysql_driver::$query

Filename: models/membership_model.php

Line Number: 8

This is m membership_model.php file

    class Membership_model extends CI_Model{

    function validate(){
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $this->$query = $this->db->query->get('members');

        if($query->num_rows != 0){
            return true;
        }
    }

}

This is my login.php file which is my main controller.

class Login extends CI_CONTROLLER{


function index()
{
    $data['main_content'] = 'login_form';
    $this->load->view('includes/template', $data);
}

function validate_credentials()
{
    //load model to query db
    $this->load->model('membership_model');
    $query = $this->membership_model->validate();

    if($query){ //if credentials validated
        $data = array(
            'username' => $this->input->post('username'),
            'is_logged_in' => true

            );

            $this ->session->set_userdata($data);
            redirect('site/members_area');
    }

    else{ //if not validated load login form again.
        $this->index();

    }

}

This is a screen shot of the error message I am getting

screen showing error message

Any ideas what could be going wrong? I haven't a clue! I've looked on the internet but nothing seems to help me. Has anyone encountered this problem. I've been following this tutorial video but it is using an older version of code igniter so I've been making changes as I go.

http://www.youtube.com/watch?v=-fLtTRYQX0M http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-6-login/

4

2 に答える 2

1
function validate(){
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));
    $this->$query = $this->db->query->get('members');

    if($query->num_rows != 0){
        return true;
    }
}

should be:

function validate()
{
    $array = array(
        'username' => $this->input->post('username'), 
        'password' => md5($this->input->post('password')));
    $query = $this->db->get_where('members', $array); 
    if ($query->num_rows() > 0)
    {
        return true;
    }
}

Note: get_where() may be called getwhere() if you are using an older version of CI

Btw you shouldn't be using MD5 for hashing your passwords. You should be using BCrypt as MD5 is insecure.

于 2013-02-14T01:08:16.233 に答える
0

First the obvious, are you loading the database library?

Second, why are you appending get() after query()?

i.e.

$this->db->query->get('members');

query() is a standalone function whose example says it is supposed to be used like this:

$query = $this->db->query('YOUR QUERY HERE');

whereas get() is part of active record.

$query = $this->db->get('mytable');

or in your case:

$query = $this->db->get('members');
于 2013-02-20T01:17:42.400 に答える