1

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?

6
  • 1
    At a guess, fetchis returning false suggesting there are no results for that query. Try var_dump($results) after the fetch to confirm. Commented Jul 11, 2020 at 10:47
  • Mmm not working, the same. bool(false) Warning: count(): Parameter must be an array or an object that implements Countable in C:\xampp\htdocs\lapapaya\login.php on line 18 Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\lapapaya\login.php on line 18. And i can login with other navigators where i have the password saved as a record. Commented Jul 11, 2020 at 10:55
  • That wasn't a fix, it was to debug your code. But it did confirm what @Jonnix suggested, your call: $records->fetch(PDO::FETCH_ASSOC); fails and returns false for some reason. You should always check the result instead of just assuming everything works. Commented Jul 11, 2020 at 10:56
  • Indeed, you've confirmed that no record is being returned by that query, and you are trying to treat a boolean (false in this case) as an array as the error tells you. Now you need to work out why there isn't data matching your query. Perhaps start by dumping out the e-mail address you're trying to sent. Commented Jul 11, 2020 at 10:57
  • 1
    Does this answer your question? My PDO Statement doesn't work Commented Jul 11, 2020 at 11:00

1 Answer 1

0

You must revalidate your assumptions whenever you receive an error like:

Trying to access array offset on value of type bool

This error means that the variable you thought was an array isn't. In this case you assume that $records->fetch(PDO::FETCH_ASSOC) returned an array, but if there are no more matching rows then this method will return return FALSE.

Usually, this kind of error is fixed with the following if statement:

$results = $records->fetch(PDO::FETCH_ASSOC);
if($results && password_verify($_POST['password'], $results['password'])) {
    // ...
}

If you actually expected $results to contain values then you need to recheck the rest of your code and the data. Check both the data in the database and the data provided in the prepared statement, to make sure that all your assumptions are correct.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.