问:我使用的是php 7,但我不知道为什么以下2种不同代码无法为用户提供用户注册页面。但是,登录页面php正常工作。我会很感激社区的反馈。
谢谢!
REG PAGE(示例1)
<!DOCTYPE html>
<html>
<head>
<title>Signup Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class = "container">
<center><h2>Signup Form</h2></center>
<form method="post" action="">
<div class="form-group">
<center><label for="username">Username:</label>
<input type="text" class="form-control" id="user" placeholder="Enter a unique username" name="username"></center>
</div>
<div class="form-group">
<center><label for="password">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter new password" name="password"></center>
</div>
<div class="form-group">
<center><label for="password">Repeat Password:</label>
<input type="password" class="form-control" id="repeat_pwd" placeholder="Repeat new password" name="password_confirmation"></center>
</div>
<div class="form-group">
<center><label for="forename">First Name:</label>
<input type="text" class="form-control" id="first_name" placeholder="Enter your First Name" name="forename"></center>
</div>
<div class="form-group">
<center><label for="surname">Surname:</label>
<input type="text" class="form-control" id="last_name" placeholder="Enter your Surname" name="surname"></center>
</div>
<div class="form-group">
<center><label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email" name="email"></center>
</div>
<div class="form-group">
<center><label for="email">Repeat Email:</label>
<input type="email" class="form-control" id="repeat_email" placeholder="Repeat your email" name="email_confirmation"></center>
</div>
<center><button type="submit" class="btn btn-default" name="submit">Register!</button></center>
<center><font color="red" size="3"><b>Already have an account ?</b><br><a href="login.php">Login here!</a></font></center>
</form>
</div>
</body>
</html>
<?php
require "conn.php";
if (isset($_POST['submit'])) {
$username = mysqli_real_escape_string($conn,$_POST["username"]);
$forename = mysqli_real_escape_string($conn,$_POST["forename"]);
$surname = mysqli_real_escape_string($conn,$_POST["surname"]);
$password = mysqli_real_escape_string($conn,$_POST["password"]);
$password_confirmation = mysqli_real_escape_string($conn,$_POST["password_confirmation"]);
$email = mysqli_real_escape_string($conn,$_POST["email"]);
$email_confirmation = mysqli_real_escape_string($conn,$_POST["email_confirmation"]);
$account_activation_code = mysqli_real_escape_string(md5($conn,$_POST["username"] + microtime()));
$account_activation = 0;
if(!$email == $email_confirmation ) {
echo "<center>Your email inputs do not match! Try inputting again and then re-submit.</center>";
exit();
$conn->close();
}
else
{
}
if(!$password_confirmation == $password_confirmation) {
echo "<center>Your password inputs do not match! Try inputting again and then re-submit.</center>";
exit();
$conn->close();
}
else
{
}
$query_1 = "INSERT INTO pending_users ('Username','Password','Forename','Surname','Email') VALUES ('$username','$password','$forename','$surname','$email')";
$mysqli->query($query_1);
$result_1 = mysqli>query($conn, $query_1);
if($result_1) {
echo "Done!";
}
else
{
echo "<center>Something is wrong! Result 1 fail.</center>";
exit();
$conn->close();
}
$query_2 = "INSERT INTO account_activation ('Username','Email','Account_Activation_Code','Account_Activation') VALUES ('$username','$email','$account_activation_code','$account_activation')";
$result_2 = mysqli>query($conn, $query_2);
if($result_2) {
echo "Done!";
}
else
{
echo "Result 2 fail.</center>";
$conn->close();
}
}
?>
------------
REG PAGE(示例2)
<!DOCTYPE html>
<html>
<head>
<title>Signup Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class = "container">
<center><h2>Signup Form</h2></center>
<form method="post" action="">
<div class="form-group">
<center><label for="username">Username:</label>
<input type="text" class="form-control" id="user" placeholder="Enter a unique username" name="username"></center>
</div>
<div class="form-group">
<center><label for="password">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter new password" name="password"></center>
</div>
<div class="form-group">
<center><label for="password">Repeat Password:</label>
<input type="password" class="form-control" id="repeat_pwd" placeholder="Repeat new password" name="password_confirmation"></center>
</div>
<div class="form-group">
<center><label for="forename">First Name:</label>
<input type="text" class="form-control" id="first_name" placeholder="Enter your First Name" name="forename"></center>
</div>
<div class="form-group">
<center><label for="surname">Surname:</label>
<input type="text" class="form-control" id="last_name" placeholder="Enter your Surname" name="surname"></center>
</div>
<div class="form-group">
<center><label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email" name="email"></center>
</div>
<div class="form-group">
<center><label for="email">Repeat Email:</label>
<input type="email" class="form-control" id="repeat_email" placeholder="Repeat your email" name="email_confirmation"></center>
</div>
<center><button type="submit" class="btn btn-default" name="submit">Register!</button></center>
<center><font color="red" size="3"><b>Already have an account ?</b><br><a href="login.php">Login here!</a></font></center>
</form>
</div>
</body>
</html>
<?php
require "conn.php";
if (isset($_POST['submit'])) {
$username = mysqli_real_escape_string($conn,$_POST["username"]);
$forename = mysqli_real_escape_string($conn,$_POST["forename"]);
$surname = mysqli_real_escape_string($conn,$_POST["surname"]);
$password = mysqli_real_escape_string($conn,$_POST["password"]);
$password_confirmation = mysqli_real_escape_string($conn,$_POST["password_confirmation"]);
$email = mysqli_real_escape_string($conn,$_POST["email"]);
$email_confirmation = mysqli_real_escape_string($conn,$_POST["email_confirmation"]);
$account_activation_code = mysqli_real_escape_string(md5($conn,$_POST["username"] + microtime()));
$account_activation = 0;
if(!$email == $email_confirmation ) {
echo "<center>Your email inputs do not match! Try inputting again and then re-submit.</center>";
exit();
$conn->close();
}
else
{
}
if(!$password_confirmation == $password_confirmation) {
echo "<center>Your password inputs do not match! Try inputting again and then re-submit.</center>";
exit();
$conn->close();
}
else
{
}
$query_1 = "INSERT INTO pending_users ('Username','Password','Forename','Surname','Email') VALUES ('$username','$password','$forename','$surname','$email')";
$mysqli->query($query_1);
$result_1 = mysqli>query($conn, $query_1);
if($result_1) {
echo "Done!";
}
else
{
echo "<center>Something is wrong! Result 1 fail.</center>";
exit();
$conn->close();
}
$query_2 = "INSERT INTO account_activation ('Username','Email','Account_Activation_Code','Account_Activation') VALUES ('$username','$email','$account_activation_code','$account_activation')";
$result_2 = mysqli>query($conn, $query_2);
if($result_2) {
echo "Done!";
}
else
{
echo "Result 2 fail.</center>";
$conn->close();
}
}
?>
----------------
登录页面(示例)
<!DOCTYPE html>
<html>
<head>
<title>Signup Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class = "container">
<center><h2>Signup Form</h2></center>
<form method="post" action="">
<div class="form-group">
<center><label for="username">Username:</label>
<input type="text" class="form-control" id="user" placeholder="Enter a unique username" name="username"></center>
</div>
<div class="form-group">
<center><label for="password">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter new password" name="password"></center>
</div>
<div class="form-group">
<center><label for="password">Repeat Password:</label>
<input type="password" class="form-control" id="repeat_pwd" placeholder="Repeat new password" name="password_confirmation"></center>
</div>
<div class="form-group">
<center><label for="forename">First Name:</label>
<input type="text" class="form-control" id="first_name" placeholder="Enter your First Name" name="forename"></center>
</div>
<div class="form-group">
<center><label for="surname">Surname:</label>
<input type="text" class="form-control" id="last_name" placeholder="Enter your Surname" name="surname"></center>
</div>
<div class="form-group">
<center><label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter your email" name="email"></center>
</div>
<div class="form-group">
<center><label for="email">Repeat Email:</label>
<input type="email" class="form-control" id="repeat_email" placeholder="Repeat your email" name="email_confirmation"></center>
</div>
<center><button type="submit" class="btn btn-default" name="submit">Register!</button></center>
<center><font color="red" size="3"><b>Already have an account ?</b><br><a href="login.php">Login here!</a></font></center>
</form>
</div>
</body>
</html>
<?php
require "conn.php";
if (isset($_POST['submit'])) {
$username = mysqli_real_escape_string($conn,$_POST["username"]);
$forename = mysqli_real_escape_string($conn,$_POST["forename"]);
$surname = mysqli_real_escape_string($conn,$_POST["surname"]);
$password = mysqli_real_escape_string($conn,$_POST["password"]);
$password_confirmation = mysqli_real_escape_string($conn,$_POST["password_confirmation"]);
$email = mysqli_real_escape_string($conn,$_POST["email"]);
$email_confirmation = mysqli_real_escape_string($conn,$_POST["email_confirmation"]);
$account_activation_code = mysqli_real_escape_string(md5($conn,$_POST["username"] + microtime()));
$account_activation = 0;
if(!$email == $email_confirmation ) {
echo "<center>Your email inputs do not match! Try inputting again and then re-submit.</center>";
exit();
$conn->close();
}
else
{
}
if(!$password_confirmation == $password_confirmation) {
echo "<center>Your password inputs do not match! Try inputting again and then re-submit.</center>";
exit();
$conn->close();
}
else
{
}
$query_1 = "INSERT INTO pending_users ('Username','Password','Forename','Surname','Email') VALUES ('$username','$password','$forename','$surname','$email')";
$mysqli->query($query_1);
$result_1 = mysqli>query($conn, $query_1);
if($result_1) {
echo "Done!";
}
else
{
echo "<center>Something is wrong! Result 1 fail.</center>";
exit();
$conn->close();
}
$query_2 = "INSERT INTO account_activation ('Username','Email','Account_Activation_Code','Account_Activation') VALUES ('$username','$email','$account_activation_code','$account_activation')";
$result_2 = mysqli>query($conn, $query_2);
if($result_2) {
echo "Done!";
}
else
{
echo "Result 2 fail.</center>";
$conn->close();
}
}
?>
答:我刚刚检查了您提到的登录代码。您在不同的示例中显示相同的代码。你为什么这样做?我想建议在html部分上方使用php代码。因此,PHP的某些功能可能会发出警告。您需要从代码中删除下面提到的行。
$mysqli->query($query_1);