2011-05-11 65 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之前可能没有正确初始化。

相关问题