简体   繁体   中英

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

I am getting this error Fatal error: Uncaught Error: Call to undefined method User::emailExists() when trying to log into my page.

I am not sure why and have tried to find a solution but no luck as of yet.

As far as I know this checks to see if the email exist (I know that email exists that I am trying to log in :-) )

Here is the code I have:

$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;
}
}

Sorry here is the user class:

<?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;
}

Thanks! :-)

Find this line in your user class public function __construct($db){ $this->conn = $db; } }public function __construct($db){ $this->conn = $db; } } And remove the last curly brace you are actually closing the class straight after the constructor. Which in turn at the end of your file you need to add another } to ensure you close the class at the end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM