I am trying to get data from a js form using php to mysql database to register new users to my university project website. I am quite new and I tried to work out what is going on but could not get it to work. Everything seems to be working but the data does not get sent to the database.
<?php
// initializing variables
$username = "";
$password = "";
$title = "";
$firstname = "";
$lastname = "";
$gender = "";
$address = "";
$postcode = "";
$email = "";
$telephone = "";
$errors = array();
$_SESSION['success'] = "";
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'cmetmarketplace');
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
$password2 = mysqli_real_escape_string($db, $_POST['password2']);
$title = mysqli_real_escape_string($db, $_POST['title']);
$firstname = mysqli_real_escape_string($db, $_POST['firstname']);
$lastname = mysqli_real_escape_string($db, $_POST['lastname']);
$gender = mysqli_real_escape_string($db, $_POST['gender']);
$address = mysqli_real_escape_string($db, $_POST['address']);
$postcode = mysqli_real_escape_string($db, $_POST['postcode']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$telephone = mysqli_real_escape_string($db, $_POST['telephone']);
// 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($password)) { array_push($errors, "Password is required"); }
if (empty($title)) { array_push($errors, "Title is required"); }
if (empty($firstname)) { array_push($errors, "First name is required"); }
if (empty($lastname)) { array_push($errors, "Last name is required"); }
if (empty($gender)) { array_push($errors, "Gender is required"); }
if (empty($address)) { array_push($errors, "Address is required"); }
if (empty($postcode)) { array_push($errors, "Postcode is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($telephone)) { array_push($errors, "Telephone number is required"); }
if ($password != $password2) {
array_push($errors, "The two passwords do not match");
}
// 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, password, title, first_name, last_name, gender, address, postcode, email, telephone)
VALUES('$username', '$password', '$title', '$firstname', '$lastname', '$gender', '$address', '$postcode', '$email', '$telephone')";
mysqli_query($db, $query);
}
}