-1

I am creating a login form using Object Oriented PHP.

The problem I am getting is when I want to output error under the field. The error message I am getting is Uncaught Error: Call to a member function errors().

Here's the code:

conn.php

<?php

class Connection {
    private $DB_HOST = 'localhost';
    private $DB_BASE = 'login_database';
    private $DB_USER = 'root';
    private $DB_PASS = '';
    protected $DB_CONN;

    public function __construct() {
        try {
            $this->DB_CONN = new PDO('mysql:host='.$this->DB_HOST.';dbname='.$this->DB_BASE.';charset=utf8', $this->DB_USER, $this->DB_PASS);
            $this->DB_CONN->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }
}

login.php

<?php

include('conn.php');

class Login extends Connection {
    private $dbEmail;
    private $dbPassword;
    private $query;
    public $error;

    public function loginFormHandler($email, $password) {
        $this->query = $this->DB_CONN->prepare("SELECT `email`, `password` FROM `users` WHERE `email` = :email AND `password` = :password");
        $this->query->bindParam(':email', $email);
        $this->query->bindParam(':password', $password);
        $this->query->execute();

        while($row = $this->query->fetch(PDO::FETCH_ASSOC)) {
            $this->dbEmail = $row['email'];
            $this->dbPassword = $row['password'];
        }

        if($this->dbEmail === $email && $this->dbPassword === $password) {
            $this->error = 'No errors found.';
        } else {
            $this->error = 'You got an error';
        }
    }

    public function errors() {
        return $this->error;
    }
}

index.php

<?php

include('class/login.php');

if(isset($_POST['email']) && isset($_POST['password'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];

    if(!empty($email) && !empty($password)) {
        $user = new Login;
        $user->loginFormHandler($email, $password);
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login Form</title>
</head>
<body>
    <div class="form">
        <form action="/oop/index.php" method="POST" accept-charset="UTF-8">
            <label for="email">Email: </label>
            <input type="email" name="email" required>
            <?php echo $user->errors(); ?>
            <label for="password">Password: </label>
            <input type="password" name="password" required>
            <input type="submit" value="Login">
        </form>
    </div>
</body>
</html>

The problem is in this line: <?php $user->errors(); ?> I instantiated the class on the top of index.php and assigned to the variable $user. So I am using the variable $user variable within index.php.

But it looks like I can't use the $user variable. How can I fix this?

tereško
  • 58,060
  • 25
  • 98
  • 150
Sanjay
  • 540
  • 2
  • 13
  • 29

1 Answers1

0

The problem is your object declaration, you have declare your object user under the if condition that will be true if user submit form.

The solution is, you have to declare your your object outside from the if statement.

Or

Alternate you can check is your user variable is set or not before echo statement