致命错误:在登录验证中调用成员函数

时间:2013-10-25 06:26:57

标签: codeigniter models codeigniter-form-helper

我有控制器文件: - login.php

class Login extends CI_Controller {
  function __construct() {
     parent::__construct();  
  }
  function success() {
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    $errorMsg ="";
    $queryResult = $this->logins->validate($username,$password);
    if($queryResult == TRUE) {
      redirect ('home'); 
    } 
    else {
      $errorMsg ="Invalid Username or Password"; 
      $this->load->view('login',$errorMsg); 
     }
  }
 }

查看: - login.php

  <script type="text/javascript">
        function validatelogin(){
            var x=document.forms["login"]["username"].value;
            var y=document.forms["login"]["passwrd"].value;
            if (x==null || x=="")
            {
                alert("First name must be filled out");
                return false;
            }
            if (y==null || y=="")
            {
                alert("Password field must be filled out");
                return false;
            }
            /*if(x!="monisha" && y!="monisha"){
                alert("Username and Password incorrect");
                return false;
            }*/
            return true;  
        }
    </script>

HTML表单包含: -

 <form name="login" id="login" action="<?php echo base_url() ?>login/success" onsubmit="return validatelogin()" method="post">

模型文件logins.php有函数,它描述了函数validate

    class Logins extends CI_Model {
        function __construct()
        {
            parent::__construct();
        }
        function validate($username,$password){
            $this->db->select('username','password');
            $this->db->from('logins');
            $this->db->where('username', $username); 
            $query = $this->db->get('logins');
            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $passwrd = $row->password;
                    if($passwrd == $password) { 
                        return TRUE; 
                    }
                }
            } else {
                return FALSE;
            }
    }
    }

但显示错误: -

致命错误:在此行中的非对象上调用成员函数validate(): -

$ queryResult = $ this-&gt; logins-&gt; validate($ username,$ password);

4 个答案:

答案 0 :(得分:0)

您没有加载“登录”类

$this->load->library('Logins');

答案 1 :(得分:0)

在模型中尝试这样

   public function __construct() {
        // Connecting Database 
        parent::__construct();
        $this->load->database();
    }

答案 2 :(得分:0)

请将您的模型类名称添加到 config \ autoload.php 文件

$ autoload ['model'] = array('logins');

它对我来说很好,请尝试一下。

答案 3 :(得分:0)

在使用之前加载名为登录的模型

$this->load->model('logins', '', TRUE);

或在autoload.php中