我已經通過代碼再次手動和使用代碼檢查程序,但無法看到我的錯誤。我正在建立一個註冊頁面,如果任何一個字段留空或者密碼不匹配,這個頁面應該會出錯。表單看起來不錯,但是當我嘗試註冊時,我收到錯誤消息「發生了以下錯誤 - 請輸入密碼,請重試。」無論我輸入什麼密碼,都會發生這種情況。註冊頁面沒有註冊用戶
此外,表單應該記住在給出錯誤消息時輸入的數據,但由於某種原因它會填寫姓氏和電子郵件字段,但不是名字。同樣,在看劇本時我看不出原因。
我正在從一本基本的書--PHP &學習MySQL,這些步驟可能並不是最好的,但我想在完成其他任務之前完成它。
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Providing a register page</title>
</head>
<body>
<?php
$page_title = 'Register' ;
include ('includes/header.html');
if ($_SERVER[ 'REQUEST_METHOD' ] == 'POST') {
require ('../connect_db.php');
$errors = array() ;
if (empty($_POST[ 'first_name' ])) {
$errors[] = 'Enter your first name.';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST[ 'first_name' ]));
}
if (empty($_POST[ 'last_name' ])) {
$errors[] = 'Enter your last name.';
} else {
$ln = mysqli_real_escape_string($dbc, trim($_POST[ 'last_name' ]));
}
if (empty($_POST[ 'email' ])) {
$errors[] = 'Enter your email address.';
} else {
$e = mysqli_real_escape_string($dbc, trim($_POST[ 'email' ]));
}
if (!empty($_POST[ 'pass1' ])) {
if ($_POST[ 'pass1' ] != $_POST[ 'pass2' ]) {
$errors[] = 'Passwords do not match.';
} else {
$p = mysqli_real_escape_string($dbc, trim($_POST[ 'pass1' ]));
}
} else {
$errors[] = 'Enter your password.';
}
if (empty($errors)) {
$q = "SELECT user_id FROM users WHERE email='$e'";
$r = mysqli_query ($dbc , $q);
if (mysqli_num_rows($r) != 0) {
$errors[] = 'Email address already registered. <a href="login.php">Login</a>';
}
}
if (empty($errors)) {
$q = "INSERT INTO users (first_name, last_name, email, pass, reg_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW()";
$r = mysqli_query ($dbc , $q);
if ($r) {
echo '<h1>Registered!</h1>'
.'<p>You are now registered.</p>'
.'<p><a href="login.php">Login</a></p>';
}
mysqli_close($dbc) ;
include ('includes/footer.html');
exit();
} else {
echo '<h1>Error!</h1>'
.'<p id="err_msg">The following error(s) occurred:<br>';
foreach ($errors as $msg) {
echo " - $msg<br>";
}
echo 'Please try again.</p>';
mysqli_close($dbc);
}
?>
<h1>Register</h1>
<form action="register.php" method="POST">
<p>
First Name: <input type="text" name="first_name"
value="<?php if (isset($_POST[ ' first_name' ])) echo $_POST[ 'first_name' ] ; ?>">
Last Name: <input type="text" name="last_name"
value="<?php if (isset($_POST[ 'last_name' ])) echo $_POST[ 'last_name' ] ; ?>">
</p><p>
Email address: <input type="text" name="email"
value="<?php if (isset($_POST[ 'email' ])) echo $_POST[ 'email' ] ; ?>">
</p><p>
Password: <input type="password name="pass1"
value="<?php if (isset($_POST[ 'pass1' ])) echo $_POST[ 'pass1' ] ; ?>">
Password: <input type="password name="pass2"
value="<?php if (isset($_POST[ 'pass2' ])) echo $_POST[ 'pass2' ] ; ?>">
</p><p>
<input type="submit" value="Register"> </p>
</form>
<?php include ('includes/footer.html') ; ?>
</body>
</html>
請使用''