如何通过在mysql php中发送电子邮件来更新忘记密码

Hi I have a registration form if I click on forgot password I am getting mail if I click on that link it is redirecting to changepassword page here I am not able to update the password for a particular user. Here is my code.Is it possible to send confirmcode in hidden value while sending forgot password mail

Sending mail:From this code I will be getting email.

$email=$_POST['email'];

if($_POST['submit']=='Send')

{

$query="select * from registered where email='$email'"; 

$result=mysql_query($query) or die(error);

if(mysql_num_rows($result))

{

$to = $_POST['email'];

$from .= 'mail@gmail.com' . "

";

$subject="Password Change link here";

$message.="Click on this link to change your password 
";

$message.="http://website.com/accounting/changepassword.php?email=$email";

$success = mail($to, $subject, $message);

echo "Forgot Password link has been sent to your email.Check your email to change your password";

}

else

{

echo "No user exist with this email id";

}

}

link: Click on this link to change your password
http://website.com/accounting/changepassword.php?email=mail@gmail.com

If I click on that link it will redirect to forgetten.php page and my code is

But I am not able to update the password can anyone help me this. If i click on that link it will redirect to this page from here it will changepassword.php

Changepassword.php

<form method="POST" action="forgotten.php" id="myform">
<input type='hidden' value="<?php echo $_GET['email'];?>" name='email'>
<div class="form-group">
<label for="psw"><span class="glyphicon"></span> Password</label>
<input id="password" class="form-control" type="password" name="password" placeholder="Enter password here" required/>
</div>
<div class="form-group">
<label for="rpsw"><span class="glyphicon"></span>Confirm Password</label>
<input id="repassword" class="form-control" type="password" name="repassword" placeholder="Retype password here" required/>
</div>
<button type="submit"  class="btn btn-success btn-block"><span class="glyphicon glyphicon-off"></span> Create New Password</button>

forgetten.php code:

    <?php

    $connection = mysql_connect("localhost", "root", "account") or       die(mysql_error());

  $db = mysql_select_db("accounting", $connection);

  $email=$_POST['email'];

  $password=$_POST['password'];

  $repassword=$_POST['repassword'];

  $sql1="SELECT * FROM registered WHERE email ='$email'";

  $result1=mysql_query($sql1);

  $query = mysql_query("update registered SET password = '$password', repassword ='$repassword' WHERE email='$email'");

 if($query)

 {

 $to = $_POST['email'];

 $from .= 'mail@gmail.com' . "

";

 $subject="Password Changed";

 $message.="your password has been changed successfully. 
";     

 $success = mail($to, $subject, $message);

 echo "Password has been changed successfully";

 }

    else{
        echo "Insertion Falied";
    }
?>

changepassword.php

  1. Replace AND with , between password & repassword.
  2. Remove ' in coloumn name email.
  3. Change 'email'=$result1 to email='$email'

Change

$query = mysql_query("update registered SET password = '$password' AND repassword ='$repassword' WHERE 'email'=$result1");

to

$query = mysql_query("update registered SET password = '$password', repassword ='$repassword' WHERE email='$email'");

For User's Requirement.

Since, you are passing email to changepassword.php. You have to create one hidden field for email, which will get submitted to forgottenpassword.php page.

Check your database connection. And, cross check whether all column name are written properly.

Use this code as it is.

ChangePassword.php

<form method="POST" action="forgotten.php" id="myform">
    <input type='hidden' value="<?php echo $_GET['email'];?>" name='email'>
    <div class="form-group">
    <label for="psw"><span class="glyphicon"></span> Password</label>
    <input id="password" class="form-control" type="password" name="password" placeholder="Enter password here" required/>
    </div>
    <div class="form-group">
    <label for="rpsw"><span class="glyphicon"></span>Confirm Password</label>
    <input id="repassword" class="form-control" type="password" name="repassword" placeholder="Retype password here" required/>
    </div>
    <button type="submit"  class="btn btn-success btn-block"><span class="glyphicon glyphicon-off"></span> Create New Password</button>
</form>

forgotten.php

<?
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("db_name", $connection);

$email=$_POST['email'];
$password=$_POST['password'];
$repassword=$_POST['repassword']; 

$sql1="SELECT * FROM registered WHERE email ='$email'";
$result1=mysql_query($sql1);

$query = mysql_query("update registered SET password = '$password', repassword ='$repassword' WHERE email='$email'");

if($query)
{
    $to = $email;
    $from = 'mail@gmail.com';
    $subject="Password Changed";
    $message.="your password has been changed successfully. ";      
    $success = mail($to, $subject, $message);
    echo "Password has been changed successfully";
}
?>

It is working

<?php
 $connection = mysql_connect("localhost", "root", "") or die(mysql_error());
 $db = mysql_select_db("db_name", $connection);

 $email=$_POST['email'];
 $password=$_POST['password'];
 $repassword=$_POST['repassword']; 

 $sql1="SELECT * FROM registered WHERE email ='$email'";
 $result1=mysql_query($sql1);

 $query = mysql_query("update registered SET password = '$password', repassword ='$repassword' WHERE email='$email'");

if($query)
{
$to = $email;
$from = 'mail@gmail.com';
$subject="Password Changed";
$message.="your password has been changed successfully. ";      
$success = mail($to, $subject, $message);
echo "Password has been changed successfully";
}

?>