2014-09-28 172 views
0

控制器/ verify_login.php頁笨無法加載模型

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Verify_login extends CI_Controller { 

function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('adminl_model') ; 
} 

function index() 
{ 
    //This method will have the credentials validation 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database'); 

    if($this->form_validation->run() == FALSE) 
    { 
    //Field validation failed. User redirected to login page 
    $this->load->view('admin/index'); 
    } 
    else 
    { 
    //Go to private area 
    redirect('about', 'refresh'); 
    } 

} 

function check_database($password) 
{ 
    //Field validation succeeded. Validate against database 
    $username = $this->input->post('username'); 

    //query the database 

    $result = $this->Admin_Model->login($username, $password); 

    if($result) 
    { 
    $sess_array = array(); 
    foreach($result as $row) 
    { 
     $sess_array = array(
     'id' => $row->id, 
     'username' => $row->username 
     ); 
     $this->session->set_userdata('logged_in', $sess_array); 
    } 
    return TRUE; 
    } 
    else 
    { 
    $this->form_validation->set_message('check_database', 'Invalid username or password'); 
    return false; 
    } 
} 
} 

型號/ adminl_model.php頁

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
Class Adminl_Model extends CI_Model 
{ 
    function __construct() 
    { 
     // Call the Model constructor 
     parent::__construct(); 
    } 
function login($username, $password) 
{ 
    $this -> db -> select('id, username, password','type'); 
    $this -> db -> from('users'); 
    $this -> db -> where('nick', $username); 
    $this -> db -> where('type', 'admin'); 
    $this -> db -> where('pass', MD5($password)); 
    $this -> db -> limit(1); 

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

    if($query -> num_rows() == 1) 
    { 
    return $query->result(); 
    } 
    else 
    { 
    return false; 
    } 
} 

}

給出錯誤。我該如何解決這個問題?我正在致力於c9.io.和stackoverflow說這是如此codely。我明白我的問題很荒謬。使用

+0

你會得到什麼錯誤? – 2014-09-28 07:58:58

+0

$ this-> load-> model('adminl_model')或者die(「error」);所以只是錯誤文本 – 2014-09-28 08:03:49

+0

和PHP給; 一個PHP錯誤遇到 嚴重性:注意 消息:未定義的屬性:Verify_login :: $ Admin_Model 文件名:控制器/ verify_login.php 行號:40 – 2014-09-28 08:06:33

回答

0

這個代碼塊:

$result = $this->Admin_Model->login($username, $password); 

必須是:

$result = $this->adminl_model->login($username, $password); 
0

$this->admin_model->login($username, $password);代替$this->Admin_Model->login($username, $password);

加載後,你會用相同的名字作爲你的類的對象訪問你的模型功能:

這裏已裝入模型$this->load->model('adminl_model') ;,所以u可以使用adminl_model代替Admin_Model

+0

我做到了,我說什麼PHP說,我添加了verify_login代碼 – 2014-09-28 08:18:33

+0

@Nurullah Macun,修改我的答案 – 2014-09-28 08:28:43