I have a login and signup form in php. I have created some users with this user signup:
<?php
require 'database.php';
$message = '';
if (!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['username'])) {
$sql = "INSERT INTO users (email, password, username) VALUES (:email, :password, :username)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':username', $_POST['username']);
$password = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt->bindParam(':password', $password);
if ($stmt->execute()) {
$message = 'El usuario ha sido creado';
header("Location: login.php");
} else {
$message = 'Debes rellenar los tres campos';
}
}
?>
I have created one user and the password match, but when i go to my login it says me this error:
Trying to access array offset on value of type bool in C:\xampp\htdocs\lapapaya\login.php on line 18 This is the line 18 in my login.php
if (count($results) > 0 && password_verify($_POST['password'], $results['password']))
And this is the complete code:
<?php
session_start();
if (isset($_SESSION['user_id'])) {
header('Location: lapapaya'.$user_id);
}
require 'database.php';
if (!empty($_POST['email']) && !empty($_POST['password'])) {
$records = $conn->prepare('SELECT id, email, password FROM users WHERE email = :email');
$records->bindParam(':email', $_POST['email']);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
$message = '';
if (count($results) > 0 && password_verify($_POST['password'], $results['password'])) {
$_SESSION['user_id'] = $results['id'];
header("Location: /");
} else {
$message = 'Lo sentimos las claves no coinciden.';
}
}
?>
Any of you could see the error?