我从在线资源中获得了这段代码,它工作正常,但是当我今天尝试使用它时,它会将用户登录到网站,但不会在表中插入任何内容。因此,如果我退出新创建的帐户,然后尝试登录,它将无法正常工作。
我的数据连接页面是server.php。注册页面是register.php
我查看了 self 错误。我什至将代码从网上重新复制到其中并替换为数据连接信息,并且仍然在做同样的事情。我想这可能是我的 server.php 但表中的所有用户帐户仍然有效,所以它不是服务器。 pgp 页面。
注册.php
<!DOCTYPE html>
<html>
<head>
<title>Registration system PHP and MySQL</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="header">
<h2>Register</h2>
</div>
<form method="post" action="register.php">
<?php include('errors.php'); ?>
<div class="input-group">
<label>Username</label>
<input type="text" name="username" value="<?php echo $username; ?>">
</div>
<div class="input-group">
<label>Email</label>
<input type="email" name="email" value="<?php echo $email; ?>">
</div>
<div class="input-group">
<label>Password</label>
<input type="password" name="password_1">
</div>
<div class="input-group">
<label>Confirm password</label>
<input type="password" name="password_2">
</div>
<div class="input-group">
<button type="submit" class="btn" name="reg_user">Register</button>
</div>
<p>
Already a member? <a href="login.php">Sign in</a>
</p>
</form>
</body>
</html>
服务器.php
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect(REMOVED FOR PUBLIC VIEWING);
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// first check the database to make sure
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "email already exists");
}
}
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: index.php');
}
}
这应该会导致用户被自动创建和登录,这就是它所做的。它没有像应该的那样插入到数据库中。应该插入
编号 用户名 电子邮件 加密密码
最佳答案
从发布的代码来看,register.php
中的表单似乎正在发布到自身,而不是 server.php
。
也许改变
<form method="post" action="register.php">
到
<form method="post" action="server.php">
[另外,一定要考虑@magnus-eriksson 的评论,这些评论与问题没有直接关系,但非常相关]
关于php - 注册页面不再插入我的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241424/