PHP密码更改脚本通过电子邮件

时间:2014-10-07 15:37:29

标签: php database passwords

连接变量将高于($ dbConnected),但出于显而易见的原因我将它们取出。至于问题,我似乎无法判断与我的数据库的连接是否存在问题,或者我的代码主体内是否存在逻辑错误。

<?php

 $hostname = "";
 $username = "";
 $password = "";

 $databaseName = "";

 $dbConnected = mysql_connect($hostname, $username, $password);

 $dbSelected = mysql_select_db($databaseName, $dbConnected);

 if ($dbConnected) {
      $email = $_POST['email'];

      $query = mysql_query("SELECT * FROM Users WHERE Primary_Email = '$email'");   
      $numrows = mysql_num_rows($query);
        // Checking to see whether the email address is registered in the database
        if ($numrows == 1) {

        $pass = rand();
        $pass = md5($pass);
        $password = $pass;

        // Updating database with new password
        mysql_query("UPDATE Users SET UserPassword = '$password' WHERE User_Email = '$email'");

        $query = mysql_query("SELECT * FROM users WHERE User_Email = '$email' AND UserPassword = '$password'");
        $numrows = mysql_num_rows($query);
        if ($numrows == 1) {
          // Create email
          $webmaster = "admin@chaluparosa-gaming.com";
          $headers = "From: Ian Monson <$webmaster>";
          $subject = "Your new password";
          $message = "Hello. You have requested a password reset. Your new password is below. Please do not reply to this email, as it was automated \n
                      Password: $password \n ";

          if (mail($email, $subject, $message, $headers)) {
            echo "Your password has been reset. An email has been sent with your new password!"
            echo '<br />';

          } else {
            echo "Error in sending out the email...";
            echo '<br />';

          }

        }

    } else {
        echo "Email address was invalid or not found...!";
    }
} else {
  echo "Error connecting to the database...!";
}

?>

1 个答案:

答案 0 :(得分:0)

您需要单独为sql添加错误消息,然后保留其余代码以显示自己的错误消息

 <?php

 $hostname = "";
 $username = "";
 $password = "";
 $databaseName = "";
 $dbConnected = mysql_connect($hostname, $username, $password) or die(mysql_error()); //die() with a mysql error message 
 $dbSelected = mysql_select_db($databaseName);
if (isset($email = $_POST['email'])) { //Check if email has been submitted
      $query = mysql_query("SELECT * FROM Users WHERE Primary_Email = '$email'") or die(mysql_error());   
      $numrows = mysql_num_rows($query);
        // Checking to see whether the email address is registered in the database
        if ($numrows == 1) {
        $pass = rand();
        $pass = md5($pass);
        $password = $pass;
        // Updating database with new password
        mysql_query("UPDATE Users SET UserPassword = '$password' WHERE User_Email = '$email'") or die(mysql_error());
        $query = mysql_query("SELECT * FROM users WHERE User_Email = '$email' AND UserPassword = '$password'") or die(mysql_error());
        $numrows = mysql_num_rows($query);
        if ($numrows == 1) {
          // Create email
          $webmaster = "admin@chaluparosa-gaming.com";
          $headers = "From: Ian Monson <$webmaster>";
          $subject = "Your new password";
          $message = "Hello. You have requested a password reset. Your new password is below. Please do not reply to this email, as it was automated \n
                      Password: $password \n ";
          if (mail($email, $subject, $message, $headers)) {
            echo "Your password has been reset. An email has been sent with your new password!"
            echo '<br />';
          } else {
            echo "Error in sending out the email...";
            echo '<br />';
          }
        }
    } else {
        echo "Email address was invalid or not found...!";
    }
}