It dosen't update the password in mydatabase mysql

Viewed 23

so i have the following code in : password-reset-token.php

<?php
if(isset($_POST['password-reset-token']) && $_POST['email'])
{
include "db.php";

$emailId = $_POST['email'];

$result = mysqli_query($conn,"SELECT * FROM users WHERE email='" . $emailId . "'");

$row= mysqli_fetch_array($result);

if($row)
{
 
 $token = md5($emailId).rand(10,9999);

 $expFormat = mktime(
 date("H"), date("i"), date("s"), date("m") ,date("d")+1, date("Y")
 );

$expDate = date("Y-m-d H:i:s",$expFormat);

$update = mysqli_query($conn,"UPDATE users set  password='" . $password . "', reset_link_token='" . $token . "' ,exp_date='" . $expDate . "' WHERE email='" . $emailId . "'");

$conn = "<a href='www.admitere.infoapp.cf/reset-password.php?key=".$emailId."&token=".$token."'>Click To Reset password</a>";


  require("PHPMailer/src/PHPMailer.php");
  require("PHPMailer/src/SMTP.php");
  require("PHPMailer/src/Exception.php");

  $mail = new PHPMailer\PHPMailer\PHPMailer();

  $mail->CharSet =  "utf-8";
  //$mail->IsSMTP();//-Din cauza asta nu mergea 
  // enable SMTP authentication
  $mail->SMTPAuth = true;                  
  // GMAIL username
  $mail->Username = "myMail";
  // GMAIL password
  $mail->Password = "MyPassword";
  $mail->SMTPSecure = "ssl";  
  // sets GMAIL as the SMTP server
  $mail->Host = "smtp.gmail.com";
  // set the SMTP port for the GMAIL server
  $mail->Port = "465";
  $mail->From='MyMail';
  $mail->FromName='AdmitereFeg';
  $mail->AddAddress($emailId, 'nume');
  $mail->Subject  =  'Reset Password';
  $mail->IsHTML(true);
  $mail->Body    = 'Click On This Link to Reset Password '.$conn.'';
  if($mail->Send())
  {
    echo "Check Your Email and Click on the link sent to your email";
  }
  else
  {
    echo "Mail Error - >".$mail->ErrorInfo;
  }
}else{
  echo "Invalid Email Address. Go back";
}
  }

  ?>

next in reset-password.php:

  enter <!doctype html>
  <html lang="en">
  <head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Reset Password In PHP MySQL</title>
  <!-- CSS -->
  <link rel="stylesheet" 
 href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
 </head>
 <body>
 <div class="container">
 <div class="card">
 <div class="card-header text-center">
 Reset Password In PHP MySQL
 </div>
 <div class="card-body">
 <?php
 if($_GET['key'] && $_GET['token'])
 {
include "db.php";
$email = $_GET['key'];
$token = $_GET['token'];
$query = mysqli_query($conn,
"SELECT * FROM `users` WHERE `reset_link_token`='".$token."' and `email`='".$email."';"
);
$curDate = date("Y-m-d H:i:s");
if (mysqli_num_rows($query) > 0) {
$row= mysqli_fetch_array($query);
if($row['exp_date'] >= $curDate){ ?>
<form action="update-forget-password.php" method="post">
<input type="hidden" name="email" value="<?php echo $email;?>">
<input type="hidden" name="reset_link_token" value="<?php echo $token;?>">
<div class="form-group">
<label for="exampleInputEmail1">Password</label>
<input type="password" name='password' class="form-control">
</div>                
<div class="form-group">
<label for="exampleInputEmail1">Confirm Password</label>
<input type="password" name='cpassword' class="form-control">
</div>
<input type="submit" name="new-password" class="btn btn-primary">
</form>
<?php } } 

}

?>

</div>
</div>
</div>
</body>
</html>
 here

Until here it works well

the problem is in : update-forget-password.php

    enter <?php
if(isset($_POST['password']) && $_POST['reset_link_token'] && $_POST['email'])
{
include "db.php";
$emailId = $_POST['email'];
$token = $_POST['reset_link_token'];
$password = md5($_POST['password']);
$query = mysqli_query($conn,"SELECT * FROM `users` WHERE `reset_link_token`='".$token."' and                         
`email`='".$emailId."'");
$row = mysqli_num_rows($query);
if($row){
mysqli_query($conn,"UPDATE users set  password='" . $password . "', reset_link_token='" . NULL 
 . "' ,exp_date='" . NULL . "' WHERE email='" . $emailId . "'");
 echo '<p>Congratulations! Your password has been updated successfully.</p>';
 }else{
 echo "<p>Something goes wrong. Please try again</p>";
 }
 }
 ?>

It says that my pass has been succsefuly updated but when i try to login in with the new password it won't match and work I really don't know what to do beucase i don't have any error either

Well I found out that it stores coreclty in my database the password I don't worry about to much about the security it's a school project (i edited md5 to hash)

So even if i change by hand the password in the database and i try to login in with that username and password it says that the username and password are inncorect (after doing the steps of forgot password)

I solved the problem in my register.php i saved my password in hash algorithm and in my update i saved in md5 andd in my register i trimmed it and in my update i didn't so the correct update-password.php is:

     <?php
        if(isset($_POST['password']) && $_POST['reset_link_token'] &&                   
       $_POST['email'])
         {
     include "db.php";
     $emailId = $_POST['email'];
     $token = $_POST['reset_link_token'];
          $password = trim($_POST["password"]);
     $query = mysqli_query($conn,"SELECT * FROM `users` WHERE 
     `reset_link_token`='".$token."' and `email`='".$emailId."'");
      $row = mysqli_num_rows($query);
     if($row){
        mysqli_query($conn,"UPDATE users set  password='" . 
      password_hash($password, PASSWORD_DEFAULT) . "', 
    reset_link_token='" . NULL . "' ,exp_date='" . NULL . "' WHERE 
       email='" . $emailId . "'");
      echo '<p>Congratulations! Your password has been updated 
   successfully.</p>';
      }else{
     echo "<p>Something goes wrong. Please try again</p>";
      }
      }
        ?>
0 Answers
Related