php - 注册页面不再插入我的数据库
问题描述
我从在线资源获得了这段代码,它运行良好,但是当我今天尝试使用时,它会将用户登录到网站,但不会在表格中插入任何内容。因此,如果我注销新创建的帐户,然后尝试登录,它将无法正常工作。
我的数据连接页面是server.php。注册页面是 register.php
我查看了自我错误。我什至将代码从在线复制到其中并替换为数据连接信息,但仍然在做同样的事情。我想可能是我的 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');
}
}
这应该会导致用户被创建并自动登录,这就是它的作用。它没有像应有的那样插入数据库。应该插入
id 用户名 电子邮件 加密密码
解决方案
从发布的代码来看,表单中的表单似乎register.php
正在发布到自身,而不是server.php
.
也许改变
<form method="post" action="register.php">
至
<form method="post" action="server.php">
[除此之外,一定要考虑@magnus-eriksson 的评论,这些评论与问题没有直接关系,但非常相关]
推荐阅读
- c# - 从多继承类中调用所有“基”类的更新、启动等的有效方法?
- android - 当游戏是 64 位时,Google Play 控制台 64 位错误
- oracle - 通过引用共享(对象类型)实例
- xamarin.forms - Xamarin.Plugin.Calender 禁用日期
- python - 如何在Python中逐步迭代地叠加绘图?
- c# - 如何在 gRPC 服务器上关闭 HTTPS
- sql - 将两个选择表达式与左连接结合起来
- c# - 如何使用 dtsearch 检索 eml 文件的电子邮件正文和附件文本?
- c# - 在 VS2019 中添加带有脚手架的控制器时出错“运行时出错...”
- angularjs - 构建混合 angular/angularjs 应用程序 - 如何在 angularjs 应用程序中加载 angular 模块