0
votes

i am new in CodeIgniter (v.2.1.4). I have a problem with getting post from model.

I have a contoller:

class Login extends CI_Controller{

    public function index(){
        $data['main_view'] = 'login_form';
        $this->load->view('login/include/template', $data);
    }

    public function validate(){

        $this->load->library('form_validation');
        $this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
        $this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');

        if($this->form_validation->run()){
            $this->load->model('users_model');

            if($this->users_model->validate_login($username, $password)){
                //valid user
            }
            else{
                //not vlaid user
            }
        }
        else{
            $this->index();
        }
    }
}

and a model:

class Users_model extends CI_Model{

    function __construct() {
        parent::__construct();
    }

    function validate_login(){
        $username = $this->input->post('user');
        $password = md5($this->input->post('pass'));

        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('users');

        if($query->num_rows() == 1){
            return TRUE;
        }
        else return FALSE; 
    }
}

When send via form (post) valid pair (username and password) nothing happens, but in apache2 log appears this:

PHP Fatal error: Call to a member function where() on a non-object in /var/www/ci/application/models/users_model.php

What is wrong?

3
Please do not use md5 for password hashing, it's not very secure. Also always use a salt when you hash passwords. I recommend you use php.net/manual/en/function.password-hash.php this instead. - Patrick

3 Answers

3
votes

Add a constructor to the Login class

class Login extends CI_Controller {
    public function __construct() {
        parent::__construct();
    }
}
0
votes

Do you have database loaded? You need to load the database first.

You can either add it to /config/autoload.php to autoload database function:

$autoload['libraries'] = array('database');

Or call on demand like this:

$this->load->database();

More details here

0
votes

Do Not Use post in model instead use this in controller like this

controller login.php

public function validate(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
    $this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');

    if($this->form_validation->run()){
        $this->load->model('users_model');

        $post = $this->input->post();

        $data['username']   =   $post['username'];
        $data['password']   =   md5($post['password']);
        if($this->users_model->validate_login($data)){
            //valid user
        }else{
            //not vlaid user
        }
    }
    else{
        $this->index();
    }
}

Model

function validate_login($where){
    $this->db->where($where);
    $query = $this->db->get('users');

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

This should work. Remember calling post somehow doesn't work in model. I dont exactly know they reason.