This question already has an answer here:
I've been staring at the code for over an hour, at the risk of coming across as an idiot, I would appreciate if someone could point out my mistake here.
<?php
#define variables and set to empty values
$first = $last = $username = $email = $password = $password_check = "";
#check if the user has clicked the button
if (isset($_POST["submit-register"])){
#get the connection to database
require "dbh.inc.php";
#retrieve information
$first = mysqli_real_escape_string($conn, $_POST["first"]);
$last = mysqli_real_escape_string($conn, $_POST["last"]);
$username = mysqli_real_escape_string($conn, $_POST["username"]);
$email = mysqli_real_escape_string($conn, $_POST["email"]);
$password = mysqli_real_escape_string($conn, $_POST["password"]);
$password_check = mysqli_real_escape_string($conn, $_POST["password_check"]);
#Error handlers
#Check for empty fields, not sure if necessary due to JavaScript
if (empty($first) || empty($last) || empty($username) ||
empty($email) || empty($password) || empty($password_check)){
header("Location: ../register.php?not_all_fields_were_filled_in");
exit();
} else { #check for valid username
if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)){
header("Location: ../register.php?signup=invalid");
exit();
} else { #check for matching passwords
if ($password != $password_check){
header("Location: ../register.php?passwords_do_not_match");
exit();
} else { #check for correct email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../register.php?signup=invalidemail");
exit();
} else { #check if the user already exists
$sql = "SELECT * FROM users WHERE username='$username' OR email='$email'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header("Location: ../register.php?signup=username_or_email_taken");
exit();
} else { #hash password and insert user
$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (Name_First, Name_Last, Username, Email, Password)
VALUES ('$first', '$last', '$username', '$email', '$hashedPwd');";
mysqli_query($conn, $sql);
header("Location: ../register.php?signup=success");
exit();
}
}
}
}
}
} else {
header("Location: ../register.php");
exit();
}
If I fill in all the fields and submit, the url says "localhost/...../signup=succes, meaning that all if statements work properly. I checked this again by leaving fields out blank, typing different passwords, invalid emails, everything.
When I check the database in phpmyadmin, it is not updated. I truly do not understand why this is the case. I could not spot any typos either.
</div>