2011-05-11 67 views
1

我一直在調試這段代碼,但仍然不成功。任何人都可以幫我嗎?致命錯誤:調用一個成員函數,其中的()對象中的一個非對象

class Membership_model extends CI_Model { 

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

function validate() 
{ 
    $this->db->where('username', $this->input->post('username')); 
    $this->db->where('password', md5($this->input->post('password'))); 
    $query = $this->db->get('membership'); 

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

function create_member() 
{ 

    $new_member_insert_data = array(
     'first_name' => $this->input->post('first_name'), 
     'last_name' => $this->input->post('last_name'), 
     'email_address' => $this->input->post('email_address'),   
     'username' => $this->input->post('username'), 
     'password' => md5($this->input->post('password'))      
    ); 

    $insert = $this->db->insert('membership', $new_member_insert_data); 
    return $insert; 
} 
} 

我一直在上線得到一個致命的錯誤

$this->db->where('username',$this->input->post('username'));

這是控制器/ login.php中

class Login extends CI_Controller { 

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

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

function validate_credentials() 
{  
    $this->load->model('membership_model'); 
    $query = $this->membership_model->validate(); 

    if($query) // if the user's credentials validated... 
    { 
     $data = array(
      'username' => $this->input->post('username'), 
      'is_logged_in' => true 
     ); 
     $this->session->set_userdata($data); 
     redirect('site/members_area'); 
    } 
    else // incorrect username or password 
    { 
     $this->index(); 
    } 
} 

function signup() 
{ 
    $data['main_content'] = 'signup_form'; 
    $this->load->view('includes/template', $data); 
} 

function create_member() 
{ 
    $this->load->library('form_validation'); 

    // field name, error message, validation rules 
    $this->form_validation->set_rules('first_name', 'Name', 'trim|required'); 
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); 
    $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email'); 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 
    $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]'); 


    if($this->form_validation->run() == FALSE) 
    { 
     $this->load->view('signup_form'); 
    } 

    else 
    {   
     $this->load->model('membership_model'); 

     if($query = $this->membership_model->create_member()) 
     { 
      $data['main_content'] = 'signup_successful'; 
      $this->load->view('includes/template', $data); 
     } 
     else 
     { 
      $this->load->view('signup_form');   
     } 
    } 

} 

function logout() 
{ 
    $this->session->sess_destroy(); 
    $this->index(); 
} 
+1

首先你需要在你的構造函數中聲明'$ this-> db' – diEcho 2011-05-11 05:50:58

+0

你能給我一個你正在告訴的聲明的樣本嗎? – nhoyti 2011-05-11 05:53:42

+0

我想你可以在加載模型時將TRUE傳遞給第三個參數來自動連接數據庫,例如'$ this-> load-> model('Model_name','',TRUE);' – John 2011-05-11 05:55:36

回答

0

難道說的load->model('membership_model')應該接受的名稱一個班級,那個名字是區分大小寫?您應該檢查API函數的返回值...

0

數據庫在調用validate之前可能沒有正確初始化。

相關問題