0

我有控制器文件:- 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 具有功能,描述功能验证

    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->logins->validate($username,$password);

4

4 回答 4

0

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

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

或在 autoload.php

于 2014-01-17T11:08:30.327 回答
0

在你的模型中尝试这样

   public function __construct() {
        // Connecting Database 
        parent::__construct();
        $this->load->database();
    }
于 2013-10-25T08:35:27.157 回答
0

您没有加载类“登录”

$this->load->library('Logins');
于 2013-10-25T06:29:54.317 回答
0

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

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

它对我来说工作正常,请尝试一下。

于 2013-10-26T06:09:03.553 回答