【发布时间】:2015-05-01 22:04:50
【问题描述】:
您好,我有未定义的属性: stdClass::$email 。你能帮帮我吗?
遇到了 PHP 错误
严重性:通知
消息:未定义的属性:stdClass::$email
文件名:controllers/verifylogin.php
行号:48
回溯:
文件:D:\wamp\www\codeigniter\application\controllers\verifylogin.php 行:48 函数:_error_handler
文件:D:\wamp\www\codeigniter\index.php 行:292 功能: 需要一次
这是验证登录控制器
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user_model','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login');
}
else
{
//Go to private area
// redirect('home', 'refresh');
}
}
public function check_database($password)
{
//Field validation succeeded. Validate against database
$email = $this->input->post('email');
//query the database
$result = $this->user_model->login($email, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'user_id' => $row->user_id,
'email' => $row->email
);
// $session_data = $this->session->set_userdata('logged_in',$sess_array);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid email or password');
return false;
}
}
}
家庭控制器
<?php
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['email'] = $session_data['email'];
$this->load->view('home', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
用户模型
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function login($email, $password){
$this->db->select('user_id','email','password');
$this->db->from('users');
$this->db->where('email',$email);
$this->db->where('password',md5($password));
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->result();
}
else {
return false;
}
}
public function register($name, $surname, $email, $password)
{
$data = array(
'name' => $name,
'surname' => $surname,
'email' => $email,
'password' => md5($password)
);
if( ($name && $surname && $email && $password) != NULL){
$query = $this->db->insert('users', $data);
}
else{
return false;
}
}
}
登录表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logowanie</title>
</head>
<body>
<h1>Widok Logowania</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<br>
<label for="password">Hasło: </label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="Zaloguj">
<?php echo form_close(); ?>
</body>
</html>
【问题讨论】:
-
如果你
var_dump($result),你会得到什么? -
当我做 var_dump($result)
array (size=1) 0 => object(stdClass)[23] public 'user_id' => string '19' (length=2)好像没有电子邮件我不知道为什么 -
第 50 行是
'email' => $row->email
标签: codeigniter properties undefined stdclass