-1

when i load my login page i see those errors:

Warning: Undefined array key "email" in C:\xampp\htdocs\alfarabi_website\login\index.php on line 8

Warning: Undefined variable $error in C:\xampp\htdocs\alfarabi_website\login\index.php on line 28

Warning: Undefined variable $success in C:\xampp\htdocs\alfarabi_website\login\index.php on line 29

and when i try to login i see another error instead of the previous one :

Trying to access array offset on value of type bool

tbh i searched over the internet but i didnt understand what is the error and how to fix it, i am new with php and sql .

here is my code :

<?php

#DB connect
$bdd = new PDO('mysql:host=localhost;dbname=alfarabi_website','root','');

#Select db
$req = $bdd -> prepare('SELECT * FROM users WHERE email = :email ');
$req -> execute(array(':email'=>$_POST['email']));
$donnees = $req -> fetch();


session_start();
if (isset($_POST['submit'])) {
    // code...
    if ($_POST['email']==$donnees['email']) {
        // code...
        $error = "";
        $success = "welcome";
        header("location: ../indexAcc.php");
    }
    else {
        // code...
        $error = "invalid";
        $success = "";
    }
}

echo $error;
echo $success;

?>

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Markus Zeller Mar 07 '21 at 18:40

1 Answers1

0

1- You need to make sure that the variable is available.

2- If the variable is an array, you should check its indexes

<?php

#DB connect
$bdd = new PDO('mysql:host=localhost;dbname=alfarabi_website','root','');

#Select db
$req = $bdd -> prepare('SELECT * FROM users WHERE email = :email ');
$req -> execute(array(':email'=>isset ($_POST['email']) ? $_POST['email'] : '') ;
$donnees = $req -> fetch();


session_start();
if (isset($_POST['submit'])) {
    // code...
    if ($_POST['email']==$donnees['email']) {
        // code...
        $error = "";
        $success = "welcome";
        header("location: ../indexAcc.php");
    }
    else {
        // code...
        $error = "invalid";
        $success = "";
    }
}

echo isset ($error) ? $error : '';
echo isset ($success) ? $success : '';

?>
abofazl rasoli
  • 116
  • 1
  • 4