我正在为我的网站创建一个register.php页面。这是我当前的页面代码:
$page_title = 'Register';
include('nav.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';
} 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);
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'];?>">
</p>
<p>
Last Name: <input type="text" name="last_name" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name'];?>">
</p>
<p>
Email: <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'];?>">
</p>
<p>
Confirm Password: <input type="password" name="pass2" value="<?php if(isset($_POST['pass2'])) echo $_POST['pass2'];?>">
</p>
<p>
<input type="submit" value="Register">
</p>
</form>`
这是我遇到的错误:
发生以下错误:
注意:未定义的变量:C:\ Abyss Web中的错误 第62行的Server \ htdocs \ register.php
警告:为C:\ Abyss Web中的foreach()提供了无效的参数 第62行的Server \ htdocs \ register.php,请重试
Array([type] => 2 [message] =>为提供了无效的参数 foreach()[file] => C:\ Abyss Web Server \ htdocs \ register.php [line] => 62)注意:未定义的变量:C:\ Abyss Web中的dbc 第67行的Server \ htdocs \ register.php
警告:mysqli_close()期望参数1为mysqli,给定null 在第67行的C:\ Abyss Web Server \ htdocs \ register.php中
我不明白为什么会出现这些错误。我尝试在最后的else语句中定义变量,但是它仍然不执行第一个if语句。我在做什么错了?