Php 未定义的属性:登录\控制器::$user
为什么会出现这个错误?我已经在图书馆里加载了数据库, 但这是一个错误 未定义的属性:登录\控制器::$user 我一直在做登录验证 这是我的自动装弹机Php 未定义的属性:登录\控制器::$user,php,codeigniter,Php,Codeigniter,为什么会出现这个错误?我已经在图书馆里加载了数据库, 但这是一个错误 未定义的属性:登录\控制器::$user 我一直在做登录验证 这是我的自动装弹机 $autoload['model'] = array(); $autoload['helper'] = array('url'); $autoload['libraries'] = array('database', 'session','form_validation'); 这是我的登录控制器 <?php defined('
$autoload['model'] = array();
$autoload['helper'] = array('url');
$autoload['libraries'] = array('database', 'session','form_validation');
这是我的登录控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('login');
}
function verify()
{
$this->load->model('user_model');
$check = $this->user->validate();
//$this->load->library('database');
if($check)
{
$this->load->view('admin/main');
}
else
{
$this->load->view('login');
$message = "Username and/or Password incorrect.\\nTry again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
感谢您的帮助更新
$this->load->model('user_model')
到$this->load->model('user_model','user')代码>
并更新行$this->db->get_where('user',$array)->result()
toreturn$this->db->get_where('user',$array)->result()代码>在用户模型中验证方法
换行
到
更新validate()
函数,如下所示
加载用户模型时$this->load->model('user_model')代码>默认情况下,它将用户模型类对象分配给Login\u Controller
中的User\u model
变量。您可以通过$this->user\u model
访问它
因此,当您使用$this->User->validate()访问User\u model
类方法validate
您收到错误未定义属性:Login\u Controller::$user
如果要使用user
variable pass第二个参数到加载模式方法,如下所示:
$this->load->model('user_model','user')代码>请不要使用代码和错误消息的屏幕截图,因此请编辑完整的错误消息。我在您的代码示例中没有看到任何$user
?请使用Line17更新对不起,第17行是$check=$this->user->validate()代码>这样来自模型的调用我已经修复了它,但无法登录$this->User_model->validate();并确保该模型是User_model.php,需要大写第一个字母
class User_model extends CI_Model
{
public function __construct() {
parent::__construct();
}
function validate()
{
$array['username'] = $this->input->post('username');
$array['password'] = $this->input->post('password');
$this->db->get_where('user',$array)->result();
}
}
$check = $this->user->validate();
$check = $this->user_model->validate();
function validate(){
$array['username'] = $this->input->post('username');
$array['password'] = $this->input->post('password');
return $this->db->get_where('user',$array)->num_rows();
}