база данных — PHP скрипт смены пароля по электронной почте

Переменные подключения были бы выше ($ 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 = "[email protected]";
$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...!";
}

?>

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 = "[email protected]";
$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...!";
}
}
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]