簡體   English   中英

為什么我會收到“調用未定義的方法 User::emailExists()”錯誤?

[英]Why am I getting a `Call to undefined method User::emailExists()` error?

我收到此錯誤Fatal error: Uncaught Error: Call to undefined method 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; } }找到這一行 public function __construct($db){ $this->conn = $db; } }並刪除你實際上是關閉類的構造函數后直奔最后的大括號。 反過來,在您的文件末尾,您需要添加另一個 } 以確保您在最后關閉課程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM