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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Pipevel
  • 69
  • 1
  • 8
  • 1
    At a guess, `fetch`is returning `false` suggesting there are no results for that query. Try `var_dump($results)` after the `fetch` to confirm. – Jonnix Jul 11 '20 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. – Pipevel Jul 11 '20 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. – M. Eriksson Jul 11 '20 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. – Jonnix Jul 11 '20 at 10:57
  • 1
    Does this answer your question? [My PDO Statement doesn't work](https://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work) – Dharman Jul 11 '20 at 11:00
  • Fatal error: Uncaught Error: Call to a member function setAttribute() on bool in C:\xampp\htdocs\lapapaya\login.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\lapapaya\login.php on line 15 when i add this: $results = $records->fetch(PDO::FETCH_ASSOC); $results->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); – Pipevel Jul 11 '20 at 11:13

1 Answers1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135