La methode $stmt->bind_param()

Fermé
janyduchemin Messages postés 36 Date d'inscription mercredi 13 mars 2019 Statut Membre Dernière intervention 6 septembre 2023 - 1 déc. 2021 à 01:03
janyduchemin Messages postés 36 Date d'inscription mercredi 13 mars 2019 Statut Membre Dernière intervention 6 septembre 2023 - 1 déc. 2021 à 13:04
Bsr,
je veux mettre en place le système de mot de passe oublié avec la mathode
$stmt->bind_param() mais j'ai cette erreur
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\wamp\www\ndameyong\forgotpassword.php on line 19
comment corriger ce problème
voilà le fichier
<?php
include 'main.php';
// Output message
$msg = '';
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if (isset($_POST['email'])) {
    // Prepare our SQL, preparing the SQL statement will prevent SQL injection.
    $stmt = $con->prepare('SELECT * FROM users WHERE email = ?');
    $stmt->bind_param('s', $_POST['email']);
    $stmt->execute();
    $stmt->store_result();
    // Check if the email exists...
    if ($stmt->num_rows > 0) {
    	$stmt->close();
        // Email exist, the $msg variable will be used to show the output message (on the HTML form)
        // Update the reset code in the database
    	$uniqid = uniqid();
        $stmt = $con->prepare('UPDATE users SET reset = ? WHERE email = ?');
        $stmt->bind_param('s', $uniqid, $_POST['email']);
        $stmt->execute();
        $stmt->close();
        // Email to send below, customize this
    	$subject = 'Password Reset';
    	$headers = 'From: ' . mail_from . "\r\n" . 'Reply-To: ' . mail_from . "\r\n" . 'Return-Path: ' . mail_from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
        // Change the link below from "yourdomain.com" to your own domain name where the PHP login system is hosted
        $reset_link = 'http://yourdomain.com/phplogin/resetpassword.php?email=' . $_POST['email'] . '&code=' . $uniqid;
    	// Feel free to customize the email message below
    	$message = '<p>Please click the following link to reset your password: <a href="' . $reset_link . '">' . $reset_link . '</a></p>';
        // Send email to the user
    	mail($_POST['email'], $subject, $message, $headers);
        $msg = 'Reset password link has been sent to your email!';
    } else {
        $msg = 'We do not have an account with that email!';
    }
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,minimum-scale=1">
		<title>Forgot Password</title>
		<link href="style.css" rel="stylesheet" type="text/css">
		<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
	</head>
	<body>
		<div class="login">
			<h1>Forgot Password</h1>
			<form action="forgotpassword.php" method="post">
                <label for="email">
					<i class="fas fa-envelope"></i>
				</label>
				<input type="email" name="email" placeholder="Your Email" id="email" required>
				<div class="msg"><?=$msg?></div>
				<input type="submit" value="Submit">
			</form>
		</div>
	</body>
</html>


Merci

6 réponses

jordane45 Messages postés 38427 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 février 2025 4 735
1 déc. 2021 à 01:22
Bonjour

Déjà il manque un s dans le premier paramètres de ton bind.. tu lui passes deux variables.. il te faut donc 2 s

Et ensuite, reset est un mot clé de mysql.
Il faut donc l'entourer de back quotes.


0
janyduchemin Messages postés 36 Date d'inscription mercredi 13 mars 2019 Statut Membre Dernière intervention 6 septembre 2023 1
1 déc. 2021 à 02:01
Merci pour votre disponibilité.

J'ai essayé d'ajouter un "s" ça crée une erreur ( Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in C:\wamp\www\ndameyong\forgotpassword.php on line 9)
j'ai utilisé les back quote sur reset rien ne change
0
jordane45 Messages postés 38427 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 février 2025 4 735
Modifié le 1 déc. 2021 à 07:03
Pourquoi ligne 9 alors que le souci concerne la ligbe 19... ?
Le s.. c'est pas sur la première requête qu'il faut le mettre hein...
0
jordane45 Messages postés 38427 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 février 2025 4 735 > jordane45 Messages postés 38427 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 février 2025
1 déc. 2021 à 09:42
lignes 18 - 19
$stmt = $con->prepare('UPDATE `users` SET `reset` = ? WHERE email = ?'); // DEUX variables " ? "
$stmt->bind_param('ss', $uniqid, $_POST['email']);  // Donc 2 "s"
0
janyduchemin Messages postés 36 Date d'inscription mercredi 13 mars 2019 Statut Membre Dernière intervention 6 septembre 2023 1
1 déc. 2021 à 11:19
Bjr,
J'ai apporté les modifications et la même erreur est toujours là.
Voilà le fichier modifié

<?php
include 'main.php';
// Output message
$msg = '';
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if (isset($_POST['email'])) {
// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
$stmt = $con->prepare('SELECT * FROM users WHERE email = ?');
$stmt->bind_param('s', $_POST['email']);
$stmt->execute();
$stmt->store_result();
// Check if the email exists...
if ($stmt->num_rows > 0) {
$stmt->close();
// Email exist, the $msg variable will be used to show the output message (on the HTML form)
// Update the reset code in the database
$uniqid = uniqid();
$stmt = $con->prepare('UPDATE `users` SET `reset` = ? WHERE email = ?'); // DEUX variables " ? "
$stmt->bind_param('ss', $uniqid, $_POST['email']);
$stmt->execute();
$stmt->close();
// Email to send below, customize this
$subject = 'Password Reset';
$headers = 'From: ' . mail_from . "\r\n" . 'Reply-To: ' . mail_from . "\r\n" . 'Return-Path: ' . mail_from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
// Change the link below from "yourdomain.com" to your own domain name where the PHP login system is hosted
$reset_link = 'http://yourdomain.com/phplogin/resetpassword.php?email=' . $_POST['email'] . '&code=' . $uniqid;
// Feel free to customize the email message below
$message = '<p>Please click the following link to reset your password: <a href="' . $reset_link . '">' . $reset_link . '</a></p>';
// Send email to the user
mail($_POST['email'], $subject, $message, $headers);
$msg = 'Reset password link has been sent to your email!';
} else {
$msg = 'We do not have an account with that email!';
}
}
?>

Merci!
0
jordane45 Messages postés 38427 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 février 2025 4 735
1 déc. 2021 à 11:40
Je suis un peu étonné, car dans tes précédentes questions tu bossais en PDO et là tu sembles être reparti en mysqli ... drôle d'idée et de choix....


Quoi qu'il en soit, tu as surement une erreur dans ta requête SQL...
L'as tu testé DIRECTEMENT dans ta bdd ( via phpmyadmin) pour t'assurer qu'elle fonctionne correctement ??

Sinon,
Ton code un peu optimisé ( avec un découpage en "fonctions" pour commencer)
<?php
include 'main.php';

//Activation affichage des erreurs Mysqli ( à placer après ta connexion à la bdd dans ton main.php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);



function userExists($email){
  global $con;
  $stmt = $con->prepare('SELECT * FROM `users` WHERE `email` = ?');
  $stmt->bind_param('s', $email);
  $stmt->execute();
  $stmt->store_result();
  $res =  $stmt->num_rows > 0 ? true : false;
  $stmt->close();
  return $res;
}

function updateReset($reset,$email){
  global $con;
  $stmt = $con->prepare('UPDATE `users` SET `reset` = ? WHERE `email` = ? ');
  if(!$stmt){
    //surement une erreur dans le nom de la table ou le nom des champs dans la requete sql..
    echo "Error in 'prepare' : " . print_r($con->error,true);
    exit;
  }
  $stmt->bind_param('ss' ,$reset ,$email );
  $res = $stmt->execute();
  $stmt->close();
  return $res;
}


// Output message
$msg = '';

//récupération PROPRE des variables AVANT de les utiliser
$email = !empty($_POST['email']) ? trim($_POST['email']) : NULL;


// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ($email) {
  if (userExists($email)) {
    $uniqid = uniqid();
    if(updateReset($uniqid,$email)){
      // Email to send below, customize this
      $subject = 'Password Reset';
      $headers = 'From: ' . mail_from . "\r\n" . 'Reply-To: ' . mail_from . "\r\n" . 'Return-Path: ' . mail_from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
      // Change the link below from "yourdomain.com" to your own domain name where the PHP login system is hosted
      $reset_link = 'http://yourdomain.com/phplogin/resetpassword.php?email=' . $_POST['email'] . '&code=' . $uniqid;
      // Feel free to customize the email message below
      $message = '<p>Please click the following link to reset your password: <a href="' . $reset_link . '">' . $reset_link . '</a></p>';
      // Send email to the user
      mail($email, $subject, $message, $headers);
      $msg = 'Reset password link has been sent to your email!';
      
    }
  } else {
    $msg = 'We do not have an account with that email!';
  } 
}
    
?>

0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
janyduchemin Messages postés 36 Date d'inscription mercredi 13 mars 2019 Statut Membre Dernière intervention 6 septembre 2023 1
1 déc. 2021 à 12:01
En effet je travaillais en PDO mais je veux apprendre une autre méthode car je souhaite vraiment maîtriser PHP (au moins deux méthodes...).
Je vais suivre vos instructions et vous faire un compte rendu
0
janyduchemin Messages postés 36 Date d'inscription mercredi 13 mars 2019 Statut Membre Dernière intervention 6 septembre 2023 1
1 déc. 2021 à 13:04
Salut,
Je viens d'essayer votre méthode,
j'ai reçu ce message (Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\ndameyong\forgotpassword.php on line 55), ce qui signifie ça marche...

Maintenant je vais vous montrer mon fichier reset.php question pour moi de me rassurer que c'est bon


<?php
include 'main.php';
// Output message
$msg = '';
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if (isset($_GET['email'], $_GET['code']) && !empty($_GET['code'])) {
// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
$stmt = $con->prepare('SELECT * FROM users WHERE email = ? AND reset = ?');
$stmt->bind_param('ss', $_GET['email'], $_GET['code']);
$stmt->execute();
$stmt->store_result();
// Check if the account exists...
if ($stmt->num_rows > 0) {
$stmt->close();
if (isset($_POST['npassword'], $_POST['cpassword'])) {
if (strlen($_POST['npassword']) > 20 || strlen($_POST['npassword']) < 5) {
$msg = 'Password must be between 5 and 20 characters long!';
} else if ($_POST['npassword'] != $_POST['cpassword']) {
$msg = 'Passwords must match!';
} else {
$stmt = $con->prepare('UPDATE accounts SET password = ?, reset = "" WHERE email = ?');
$password = password_hash($_POST['npassword'], PASSWORD_DEFAULT);
$stmt->bind_param('ss', $password, $_GET['email']);
$stmt->execute();
$stmt->close();
$msg = 'Password has been reset! You can now <a href="index.php">login</a>!';
}
}
} else {
exit('Incorrect email and/or code!');
}
} else {
exit('Please provide the email and code!');
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1">
<title>Reset Password</title>
<link href="style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
</head>
<body>
<div class="login">
<h1>Reset Password</h1>
<form action="resetpassword.php?email=<?=$_GET['email']?>&code=<?=$_GET['code']?>" method="post">
<label for="npassword">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="npassword" placeholder="New Password" id="npassword" required>
<label for="cpassword">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="cpassword" placeholder="Confirm Password" id="cpassword" required>
<div class="msg"><?=$msg?></div>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>



Merci!
0