I have an assignment due in 2 days and I’m tearing my hair out because I have been stuck on this same problem for 3 days now… does anyone know why this won’t work?
This is for a registration form…
<?php
if (isset($_POST['register-submit'])) {
require 'connect.inc.php';
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$HouseNumber = $_POST['HouseNumber'];
$Street = $_POST['Street'];
$TownCity = $_POST['TownCity'];
$Postcode = $_POST['Postcode'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
$PasswordRepeat = $_POST['Password-Repeat'];
if (empty($FirstName)
|| empty($LastName)
|| empty($HouseNumber)
|| empty($Street)
|| empty($TownCity)
|| empty($Postcode)
|| empty($Phone)
|| empty($Email)
|| empty($Password)
|| empty($PasswordRepeat)) {
header("Location: ../register.php?error=empty_fields&FirstName=" . $FirstName . "&LastName=" . $LastName . "&HouseNumber=" . $HouseNumber . "&Street=" . $Street . "&TownCity=" . $TownCity . "&Postcode=" . $Postcode . "&Phone=" . $Phone . "&Email=" . $Email);
exit();
// Valid Email Check
} else if (!filter_var($Email, FILTER_SANITIZE_EMAIL)) {
header("Location: ../register.php?error=invalid_email&FirstName=" . $FirstName . "&LastName=" . $LastName . "&HouseNumber=" . $HouseNumber . "&Street=" . $Street . "&TownCity=" . $TownCity . "&Postcode=" . $Postcode . "&Phone=" . $Phone);
exit();
} else if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../register.php?error=invalid_email&FirstName=" . $FirstName . "&LastName=" . $LastName . "&HouseNumber=" . $HouseNumber . "&Street=" . $Street . "&TownCity=" . $TownCity . "&Postcode=" . $Postcode . "&Phone=" . $Phone);
exit();
// Password Match Check
} else if ($Password !== $PasswordRepeat) {
header("Location: ../register.php?error=check_password&FirstName=" . $FirstName . "&LastName=" . $LastName . "&HouseNumber=" . $HouseNumber . "&Street=" . $Street . "&TownCity=" . $TownCity . "&Postcode=" . $Postcode . "&Phone=" . $Phone);
exit();
// Email Already Exists Check
} else {
$sql = "SELECT Email FROM users WHERE Email = ?";
echo $sql;
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../register.php?error=sqlerror");
exit();
} else {
mysqli_stmt_bind_param($stmt, 's', $Email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if ($resultCheck > 0) {
header("Location: ../register.php?error=email_already_registered&FirstName=" . $FirstName . "&LastName=" . $LastName . "&HouseNumber=" . $HouseNumber . "&Street=" . $Street . "&TownCity=" . $TownCity . "&Postcode=" . $Postcode . "&Phone=" . $Phone);
exit();
} else {
// INSERT INTO address
$sql = "INSERT INTO address (HouseNumber, Street, TownCity, Postcode) VALUES (?, ?, ?, ?)";
$stmt = $conn->mysqli_stmt_init($sqlinsert);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../register.php?error=sqlerror=insert_into_address");
exit();
} else {
mysqli_stmt_bind_param($stmt, "ssss", $HouseNumber, $Street, $TownCity, $Postcode);
mysqli_stmt_execute($stmt);
$latest_id = $conn->insert_id;
echo "Insert successful. Latest ID is: " . $latest_id;
}
// INSERT INTO users
$sql = "INSERT INTO users (FirstName, LastName, AddressID, Phone, Email, Password) VALUES (?, ?, '$latest_id', ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../register.php?error=sqlerror=insert_into_users");
exit();
} else {
$hashedPassword = password_hash($Password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "ssisss", $FirstName, $LastName, $AddressID, $Phone, $Email, $hashedPassword);
mysqli_stmt_execute($stmt);
header("Location: ../register.php?registered=true");
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}