Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

php - Values are not getting fetched in admin login process in codeigniter

I Have created a controller named Admin.php

class Admin extends CI_Controller { 


function __construct(){
    parent::__construct();
}
public function index($msg = NULL) {
     $data['msg'] = $msg;

    $this->load->view('pages/sign-in');

}
 public function process(){
    // Load the model
    $this->load->model('admin/Admin_model');
    // Validate the user can login
    $result = $this->Admin_model->validate();
    // Now we verify the result
   if(! $result){
        // If user did not validate, then show them login page again
        $msg = '<font color=red>Invalid username and/or password.</font><br />';
        $this->index($msg);
    }else{
        // If user did validate, 
        // Send them to members area
        redirect(base_url().'home');
    }      

}

and model is

 class Admin_model extends CI_Model
 {
 function __construct()
 {
      // Call the Model constructor
      parent::__construct();
       $this->load->database();
 }
 public function validate(){
    // grab user input
    $username = $this->security->xss_clean($this->input->post('username'));
    $password = $this->security->xss_clean($this->input->post('password'));

    // Prep the query
    $this->db->where('username', $username);
    $this->db->where('password', $password);

    // Run the query
    $query = $this->db->get('wc_watchconnect_adminlogin');
    // Let's check if there are any results
    if($query->num_rows == 1)
    {
        // If there is a user, then create session data
        $row = $query->row();
        $data = array(
                'userid' => $row->id,

                'username' => $row->username,
                'validated' => true
                );
        $this->session->set_userdata($data);
        return true;
    }
    // If the previous process did not validate
    // then return false.
    return false;
}

}

one more controller is home

class Home extends CI_Controller { 


function __construct(){
    parent::__construct();
     $this->check_isvalidated();
}

 public function index(){
    $this->load->view('pages/index');
}
private function check_isvalidated(){
    if(! $this->session->userdata('validated')){
        redirect(base_url().'index.php/admin');
    }
}
}
?>

in this $result is showing null value it is blank while i have username=admin and password=admin in database ... values are not getting fetch in $result pls help me to get rid out of this... thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to change

if($query->num_rows == 1)

To

if($query->num_rows() == 1)

Add () after num_rows

Read num_rows()

if ($query->num_rows() == 1) {
    // If there is a user, then create session data
    $row = $query->row();
    $data = array(
        'userid' => $row->id,
        'username' => $row->username,
        'validated' => true
    );
    $this->session->set_userdata($data);
    return true;
} else {
    // If the previous process did not validate
    // then return false.
    return false;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...