I'm creating a registration and login page using PHP. I've written code the registration page but due to some reasons, the data that I have tried to insert in the registration page is not being added to the database. It does not show any errors and also does not redirect to the other page.
I've consulted many videos and codes related to this project but none of them have worked.
<?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');
}
}
There is no error message displayed. The only problem is that the data are not added to the database.