Php 为什么我得到一个`调用未定义的方法User::emailExists()`错误?
我在尝试登录到我的页面时遇到此错误Php 为什么我得到一个`调用未定义的方法User::emailExists()`错误?,php,Php,我在尝试登录到我的页面时遇到此错误致命错误:未捕获错误:调用未定义的方法User::emailExists() 我不知道为什么,并试图找到一个解决办法,但没有运气到目前为止 据我所知,这将检查电子邮件是否存在(我知道我正在尝试登录的电子邮件存在:-) 以下是我的代码: $database = new Database(); $db = $database->getConnection(); // initialize objects $user = new User($db); //
致命错误:未捕获错误:调用未定义的方法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;
}
在用户类中查找此行
公共函数uu构造($db){$this->conn=$db;
}
}
移除最后一个大括号,实际上是在构造函数之后直接关闭类。在文件末尾,您需要添加另一个}以确保在结尾关闭类您需要向我们显示具有emailExists函数的用户类,我已更新了我的问题:-)这就是明确说明方法是public
有帮助的地方。我现在真的觉得:-(谢谢!!现在一切都很好。我现在可以继续我的生活了:-!!欢迎你,请接受正确的答案:)我刚刚有。两分钟后我才能接受。再次感谢!!