sunny
sunny

Reputation: 1593

User Validation is not working in CodeIgniter

I am new in codeigniter. I create an login form and i want to validate my login credentials. I search many solutions but i failed. I cannot validate my credentials.

Here is My view Code name as login_form.php

<div id="login_form">
        <h1>Login Page..!!</h1>
        <?php
        echo form_open('login/validate_credentials');
        echo form_input('username', 'Username');
        echo form_password('password', 'Password');
        echo form_submit ('submit', 'Login');
        ?>

    </div>

Here is My Model Code name as users_model.php

<?php
class Users_model extends CI_Model{

        function validate(){

                $this->db->where('username', $this->input->post('username'));
                $this->db->where('password', $this->input->post('password'));
                $query = $this->db->get('users');
if ($query->num_rows == 1){
                    return true;
                    }

            }

    } ?>

Here is My Controler Code name as Login.php

<?php

    class Login extends CI_Controller{

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

        function validate_credentials(){

            $this->load->model('users_model');
            $query = $this->users_model->validate();




            if($query){


                $data = array(
                                'username'=> $this->input->post('username'),
                                'is_logged_in' => true
                );

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

            }

            else{
                echo "Wrong";
                //$this->index();   
            }
        }
    }
?>

Even when i give it right username and password it cannot validate and echo "Wrong" as shown in my code

Upvotes: 0

Views: 26

Answers (1)

Pradeep
Pradeep

Reputation: 9717

num_rows() should be a method

change to it:

  function validate()
  {

            $this->db->where('username', $this->input->post('username'));
            $this->db->where('password', $this->input->post('password'));
            $query = $this->db->get('users');
            if ($query->num_rows() == 1){
                return true;
                }

  }

https://www.codeigniter.com/user_guide/database/results.html#id4

Upvotes: 1

Related Questions