Reputation: 5
I'm working on a program to update the user's password with PHP/MySQL. The table is titled 'utilisateurs' and contains several fields:
• numero (auto-increment);
• identifiant (user ID);
• motdepasse (password).
The user has to provide the actual password ($ancienmdp) and to type the new password twice ($nouveaumdp1 and $nouveaumdp2) and then submit the form.
Here's the part of program I'm working on and for which, despite my investigations, I still haven't found any solution:
//On récupère le mot de passe actuel et les deux nouveaux mots de passe entrés par l'utilisateur.
$ancienmdp = $_POST['ancienmdp'];
$nouveaumdp1 = $_POST['nouveaumdp1'];
$nouveaumdp2 = $_POST['nouveaumdp2'];
if ($nouveaumdp1 != $nouveaumdp2) { //Si les deux nouveaux mots de passe ne correspondent pas...
//... un message d'erreur s'affiche pour indiquer que les deux nouveaux mots de passe ne correspondent pas.
$message = '<p class="normal">Le nouveau mot de passe et sa confirmation ne correspondent pas ! Veuillez taper à nouveau votre nouveau mot de passe.</p>';
} else { //Si les deux nouveaux mots de passe correspondent...
//On recherche le mot de passe actuel de l'utilisateur.
$identifiantrecherche = $_SESSION['identifiant'];
$q= $dbco->prepare("SELECT motdepasse FROM `utilisateurs` WHERE identifiant=?");
$q->execute([$identifiantrecherche]);
$actuelmdp = $q->fetchColumn();
if ($ancienmdp != $actuelmdp) { //Si le mot de passe actuel entré ne correspond pas à celui déjà enregistré...
//... un message d'erreur indique que le mot de passe actuel n'est pas valide.
$message = '<p class="normal">Mot de passe actuel invalide ! Veuillez taper à nouveau vos actuel et nouveau mots de passe.</p>';
} else { //S'ils correspondent bien...
//... on met à jour le mot de passe de l'utilisateur...
$sth = $dbco->prepare("UPDATE `utilisateurs` SET `motdepasse` = :nouveaumdp1 WHERE `identifiant` = :identifiantrecherche");
//echo '$identifiant = ' . $identifiantrecherche . ' ; $motdepasse = ' . $nouveaumdp1;
$sth->bindParam(':identifiant',$identifiantrecherche, PDO::PARAM_STR);
$sth->bindParam(':motdepasse',$nouveaumdp1, PDO::PARAM_STR);
$sth->execute();
//... et on indique à ce dernier que son mot de passe a bien été modifié.
$message = '<p class="normal">Mot de passe modifié !</p>';
} }
I get the error:
'Erreur !: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'
The 'motdepasse' field has to be updated and the 'identifiant' field is used to find the right user. So, I use bindParam for these two fields.
What am I doing wrong?
Thanks!
Upvotes: 0
Views: 786
Reputation: 780714
The placeholders in bindParam()
need to match the placeholders in the query.
$sth->bindParam(':identifiantrecherche',$identifiantrecherche, PDO::PARAM_STR);
$sth->bindParam(':nouveaumdp1',$nouveaumdp1, PDO::PARAM_STR);
Upvotes: 0
Reputation: 5
Thanks to @TimBrownlaw, I stated the second query with positional placeholders like the first query is, and not with named placeholders as I did.
The second query becomes:
$sth = $dbco->prepare("UPDATE `utilisateurs` SET motdepasse=? WHERE identifiant=?");
$sth->execute([$nouveaumdp1,$identifiantrecherche]);
Now it works!
Thanks for your help and your time!
Upvotes: 0