我正在使用PHP创建注册和登录页面。我已经编写了注册页面的代码,但是由于某些原因,我尝试插入注册页面的数据并未添加到数据库中。它不会显示任何错误,也不会重定向到其他页面。
我已经查阅了许多与此项目有关的视频和代码,但是都没有用。
<?php
session_start();
$username = "";
$email = "";
$errors = [];
$db = mysqli_connect('localhost', 'root', '', 'joblister');
if (isset($_POST['submit'])) {
$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']);
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");
}
// a user does not already exist with the same username and/or email
$jobseeker_check_query = "SELECT * FROM jobseeker WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($db, $jobseeker_check_query);
$jobseeker = mysqli_fetch_assoc($result);
if ($jobseeker) { // if user exists
if ($jobseeker['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($jobseeker['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);
$query = "INSERT INTO jobseeker (firstname, lastname, gender, email, username password, address, contact,dob)
VALUES('$firstname','$lastname','$gender','$email','username','$password', '$address','$contact','dob')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: FirstPage.php');
}
}
没有显示错误信息。唯一的问题是数据没有添加到数据库中。
答案 0 :(得分:1)
// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);
$query = "INSERT INTO jobseeker (firstname, lastname, gender, email, **username password**, address, contact,dob)
VALUES('$firstname','$lastname','$gender','$email','username','$password', '$address','$contact','dob')";
// ... rest of the code
}
我相信您需要在用户名和密码之间加上逗号。