为什么我收到"调用未定义的方法 User::emailExists()"错误?



我在尝试登录我的页面时Fatal error: Uncaught Error: Call to undefined method User::emailExists()收到此错误。

我不确定为什么,并试图找到解决方案,但到目前为止还没有运气。

据我所知,这会检查电子邮件是否存在(我知道电子邮件存在,我正在尝试登录:-((

这是我的代码:

$database = new Database();
$db = $database->getConnection();
// initialize objects
$user = new User($db);
// check if email and password are in the database
$user->email=$_POST['email'];
// check if email exists, also get user details using this emailExists() 
method
$email_exists = $user->emailExists(); --> Error at this line
// validate login
if ($email_exists && password_verify($_POST['password'], $user->password) 
&& $user->status==1){
// if it is, set the session value to true
$_SESSION['logged_in'] = true;
$_SESSION['user_id'] = $user->id;
$_SESSION['access_level'] = $user->access_level;
$_SESSION['firstname'] = htmlspecialchars($user->firstname, ENT_QUOTES, 
'UTF-8') ;
$_SESSION['lastname'] = $user->lastname;
// if access level is 'Admin', redirect to admin section
if($user->access_level=='Admin'){
header("Location: {$home_url}admin/index.php?action=login_success");
}
// else, redirect only to 'Customer' section
else{
header("Location: {$home_url}index.php?action=login_success");
}
}
// if username does not exist or password is wrong
else{
$access_denied=true;
}
}

对不起,这里是用户类:

<?php
// 'user' object
class User{
// database connection and table name
private $conn;
private $table_name = "users";
// object properties
public $id;
public $firstname;
public $lastname;
public $email;
public $contact_number;
public $address;
public $password;
public $access_level;
public $access_code;
public $status;
public $created;
public $modified;
// constructor
public function __construct($db){
$this->conn = $db;
}
}
function emailExists(){
// query to check if email exists
$query = "SELECT id, firstname, lastname, access_level, password, status
FROM " . $this->table_name . "
WHERE email = ?
LIMIT 0,1";
// prepare the query
$stmt = $this->conn->prepare( $query );
// sanitize
$this->email=htmlspecialchars(strip_tags($this->email));
// bind given email value
$stmt->bindParam(1, $this->email);
// execute the query
$stmt->execute();
// get number of rows
$num = $stmt->rowCount();
// if email exists, assign values to object properties for easy access and 
use for php sessions
if($num>0){
// get record details / values
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// assign values to object properties
$this->id = $row['id'];
$this->firstname = $row['firstname'];
$this->lastname = $row['lastname'];
$this->access_level = $row['access_level'];
$this->password = $row['password'];
$this->status = $row['status'];
// return true because email exists in the database
return true;
}
// return false if email does not exist in the database
return false;
}

谢谢! :-(

在用户类中找到此行public function __construct($db){ $this->conn = $db; } }并删除最后一个大括号,您实际上是在构造函数之后直接关闭类。反过来,在文件末尾,您需要添加另一个 } 以确保在最后关闭类

最新更新