在codeigniter

时间:2016-07-06 07:15:30

标签: php codeigniter

我创建了一个名为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');
    }      

}

和模型是

 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;
}

}

还有一个控制器在家

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');
    }
}
}
?>

在这个$ result中显示空值,它是空白的,而我在数据库中有username = admin和password = admin ...值没有得到$ result中的结果请帮我摆脱这个...谢谢提前

1 个答案:

答案 0 :(得分:1)

您需要更改

if($query->num_rows == 1)

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

()

之后添加num_rows

阅读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;
}