我正在嘗試爲我嘗試創建的網站創建密碼更改頁面。但是,當用戶輸入信息到表單時,它會將我發送到空白頁面。換句話說,我的PHP代碼甚至沒有執行,我不知道爲什麼。我嘗試了幾種不同的方式,但我不完全確定發生了什麼。這是我第一次做一個設置頁面或網站,所以也許它是一個簡單的錯誤,或者我正在做的全部錯誤。這是我的PHP代碼。Php中的密碼更改,Php返回空白
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
$db_server = "server";
$db_username = "name";
$db_password = "pass";
$con = mysql_connect($db_server, $db_username, $db_password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$database = "Account_Holder";
$er = mysql_select_db($db_username);
if (!$er) {
print ("Error - Could not select the database");
exit;
}
$username = $_P0ST['username'];
$cur_password = $_POST['cur_password'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
// Check for an existing password.
if (isset($_POST['cur_password']))
{
$pass = FALSE;
print('<p>You forgot to enter your existing password!</p>');
}
else {
$pass = escape_data($_POST['cur_password']);
}
// Check for a password and match against the confirmed password.
if (empty($_POST['password']))
{
$newpass = FALSE;
print('<p>You forgot to enter your new password!</p>');
}
else
{
if ($_POST['password'] == $_POST['password2']) {
$newpass = escape_data($_POST['password']);
}
else
{
$newpass = FALSE;
$message .= '<p>Your new password did not match the confirmed new password!</p>';
}
if ($pass && $newpass) { // If checking passes
$query = "SELECT * FROM Account_Holder WHERE password='$pass')";
$result = mysql_query($query);
$num = mysql_num_rows($result);
if ($num == 1) {
$row = mysql_fetch_array($result);
// Make the query.
$query = ("UPDATE Account_Holder SET password='$newpass' WHERE username=$username");
$result = mysql_query($query); // Run the query.
if (mysql_affected_rows() == 1) { // If it ran OK.
echo '<p><b>Your password has been changed.</b></p>';
}
else
{ // If query failed.
print('<p>Your password was not changed.</p><p>' . mysql_error() . '</p>');
}
} else
{
print('<p>Your username and password did not match any in the database.</p>');
}
}
else
{
print('<p>Please try again.</p>');
}
}
?>
</body>
</html>
<!--
I also did it this way and all the validations work and it says the password was updated but it does not change in the database. Is something wrong with my sql?
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Change Password Confrim</title>
</head>
<body>
<?php
$db_server = "server";
$db_username = "name";
$db_password = "pass";
$con = mysql_connect($db_server, $db_username, $db_password);if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$database = "Account_Holder";
$er = mysql_select_db($db_username);
if (!$er)
{
print ("Error - Could not select the database");
exit;
}
//include "include/session.php";
// check the login details of the user and stop execution if not logged in
//require "check.php";
//////////Collect the form data ////////////////////
$username =$_P0ST['username'];
$todo=$_POST['todo'];
$password=$_POST['password'];
$password2=$_POST['password2'];
/////////////////////////
if(isset($todo) and $todo=="change-password"){
$password = mysql_real_escape_string($password);
$password2 = mysql_real_escape_string($password2);
//Setting flags for checking
$status = "OK";
$msg="";
//Checking to see if password is at least 3 char max 8
if (strlen($password) < 3 or strlen($password) > 8)
{
$msg=$msg."Password must be more than 3 char legth and maximum 8 char lenght<br/>";
$status= "NOTOK";
}
//Checking to see if both passwords match
if ($password <> $password2)
{
$msg=$msg."Both passwords are not matching<br/>";
$status= "NOTOK";
}
if($status<>"OK")
{
echo "<font face='Verdana' size='2' color=red>$msg</font><br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";
}
else
{ // if all validations are passed.
if(mysql_query("UPDATE Account_Holder SET password='$password' WHERE username='$username'"))
{
echo "<font face='Verdana' size='2' ><center>Thanks <br> Your password changed successfully.</font></center>";
}
else
{
echo "<font face='Verdana' size='2' color=red><center>Sorry <br> Failed to change password.</font></center>";
}
}
}
//require "bottom.php";
?>
<center>
<br><br><a href='Settings.html'>Settings Page</a></center>
</body>
</html>
你可能想看看你的例如Apache日誌,PHP可能會抱怨有錯誤 – daisy
你在$ _POST ['username']中捕獲0嗎? –
正如@MorganDelaney所指出的那樣,應該有一個0'$ username = $ _P0ST ['username'];' –