我已经创建了一个注册和登录页面。它第一次工作得很好,我在注册表单中输入的数据被插入到我的数据库中。但是当我第二次尝试时,mysql没有将数据输入到表中。我尝试了几次,但是表格只显示一行,并且没有插入更多的行。
这是将数据插入数据库的代码
<?php
session_start();
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to the data base
$db = mysqli_connect('localhost', 'root', '', 'registration');
// 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: profile.php');
}
}
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: profile.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>表单的Html代码:
<?php include('server.php') ?>
///...
<body>
<div class="form">
<ul class="tab-group">
<li class="tab active"><a href="#signup">Sign Up</a></li>
<li class="tab"><a href="#login">Log In</a></li>
</ul>
<div class="tab-content">
<div id="signup">
<h1>Sign Up for Free</h1>
<form action="register.php" method="post">
<?php include('errors.php'); ?>
<div class="field-wrap">
<label>
User Name<span class="req">*</span>
</label>
<input type="text" name="username" value="<?php echo $username; ?>"required autocomplete="off" />
</div>
<div class="field-wrap">
<label>
Email Address<span class="req">*</span>
</label>
<input type="email" name="email" value="<?php echo $email; ?>" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Set A Password<span class="req">*</span>
</label>
<input type="password" name="password_1" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Set A Password Again<span class="req">*</span>
</label>
<input type="password" name="password_2" required autocomplete="off"/>
</div>
<button type="submit" class="button button-block" name="reg_user"/>Get Started</button>
</form>
</div>
<div id="login">
<h1>Welcome Back!</h1>
<form action="register.php" method="post">
<div class="field-wrap">
<label>
User name<span class="req">*</span>
</label>
<input type="text" name="username" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Password<span class="req">*</span>
</label>
<input type="password" name="password"required autocomplete="off"/>
</div>
<button class="button button-block" name="login_user"/>Log In</button>
</form>
</div>
</div><!-- tab-content -->
</div> <!-- /form -->
<script src="register.js"></script>
</body>
</html>可以得到一些关于这个问题的帮助,请检查并帮助我解决这个问题
发布于 2019-08-21 23:08:57
正如我已经在评论中建议的,像这样的问题,如果一行插入正常,那么下一行通常与此无关-您的主键行不是AUTO_INCREMENT,这会导致重复的主键和新的插入被拒绝(这就是问题所在)-或者不应该是唯一的行被定义为唯一的(例如,将密码行设置为唯一不是一个好主意)。
它与PHP没有任何关系,但是您需要在数据库中修复它。如果您使用PHPMyAdmin,那么选中您的主键的a_i复选框,并确保应该有索引但不应该是唯一的或主键设置为索引的行。
在普通SQL中,可以像在CREATE TABLE whatever (ID INT(16) PRIMARY KEY NOT NULL AUTO_INCREMENT);中一样使用AUTO_INCREMENT关键字。至于在MySQL中创建适当的索引,请查看the manual,如果它造成麻烦,请避免使用关键字unique。
https://stackoverflow.com/questions/57594004
复制相似问题