-5

I am working on an authentication form, and this is the HTML code:

<input type="text" class="form-control" name="matriculeAgent" placeholder="Compte matriculaire">

<input type="password" class="form-control password" name="motdepasseAgent" placeholder="mot de passe">

This is the PHP code where I compare the matriculeAgent and motdepasseAgent with the information of the database:

$matricule_verif = $_POST["matriculeAgent"] ; 
//password: 
$motdepasse_verif = $_POST["motdepasseAgent"] ; 
if ((isset($_POST["matriculeAgent"])==true) && (isset($_POST["motdepasseAgent"])==true)){
$req = $bdd->prepare("SELECT `matriculeAgent`, `motdepasseAgent` FROM `utilisateurs` WHERE `matriculeAgent` ='".$matricule_verif.'"AND `motdepasseAgent` = "'.$motdepasse_verif."'");
  // SI AUCUN ENREGISTREMENT NE CORRESPOND 
if(mysql_num_rows($req)==0) 
{ 
echo "Aucun utilisateurs ayant le mot de passe saisi existe. Reéssayer"; 
} 


// SI LE LOGIN ET MOT DE PASSE SONT EXACTES 
else 
{ 
    $_SESSION['matriculeAgent'] = $row['matriculeAgent'];
    $_SESSION['motdepasseAgent'] = $row['motdepasseAgent'];
// REDIRECTION VERS UNE PAGE PROTEGEE 
header("Location:accueil.php");
// selection de lenregistrement saisie a la page login.php 
}

}
else{

    echo "veuillez saisir le motdpass";
}

This are the errors I am getting:

Notice: Undefined index: matriculeAgent in C:\Users\admin\Dropbox\EasyPHP-DevServer-14.1VC9\data\localweb\esmcm\dossier\makeLogin.php on line 25

Notice: Undefined index: motdepasseAgent in C:\Users\admin\Dropbox\EasyPHP-DevServer-14.1VC9\data\localweb\esmcm\dossier\makeLogin.php on line 27

Panda
  • 6,955
  • 6
  • 40
  • 55
ilfto
  • 1
  • 4
    Possible duplicate of http://stackoverflow.com/questions/26339949/post-undefined-index-php-error?s=5|2.4276 and many others. – Julie Pelletier May 15 '16 at 01:06
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Kara Aug 10 '16 at 22:02

1 Answers1

0

it looks like $row['matriculeAgent'] is not set. this could be caused by multiple causes. I noticed several problems in your code, which I try to explain here.

first of all, if you want to you prepared statements, dont include your parameters directly inside the stmt->prepare() function call, use bind-param instead:

$stmt = $bdd->prepare("SELECT `matriculeAgent`, `motdepasseAgent` FROM `utilisateurs` WHERE `matriculeAgent` = ? AND `motdepasseAgent` = ?");
$stmt->bind_param("ss", $matricule_verif, $motdepasse_verif);

then, another problem may be that you did not execute the statement, or you didn't show it in the post.

$stmt->execute();

after that, you can simply fetch your results, it should then be correctly populated:

$result = $stmt->get_result();

another way after execute(): as you are already using prepared statements, you could use here also bind_result(), which will populate your desired variables:

$varA = '';
$varB = '';
$stmt->bind_result($varA, $varB);
$stmt->fetch();
Jack O'Neill
  • 1,032
  • 2
  • 19
  • 34