connect_error) { die("Connection failed: " . $conn->connect_error); } // Check if the form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve and sanitize form inputs $email = mysqli_real_escape_string($conn, $_POST['email']); $first_name = mysqli_real_escape_string($conn, $_POST['firstname']); $last_name = mysqli_real_escape_string($conn, $_POST['lastname']); $password = mysqli_real_escape_string($conn, $_POST['password']); $cpassword = mysqli_real_escape_string($conn, $_POST['cpassword']); $referral_code = !empty($_POST['referral_code']) ? mysqli_real_escape_string($conn, $_POST['referral_code']) : null; // Check if referral code is provided // Check if passwords match if ($password !== $cpassword) { echo "Passwords do not match."; exit(); } // Hash the password for secure storage $hashed_password = password_hash($password, PASSWORD_BCRYPT); // Prepare the SQL statement with or without referral code if ($referral_code) { // If referral code is provided $stmt = $conn->prepare("INSERT INTO users (email, first_name, last_name, password_hash, referral_code) VALUES (?, ?, ?, ?, ?)"); $stmt->bind_param("sssss", $email, $first_name, $last_name, $hashed_password, $referral_code); } else { // If referral code is not provided $stmt = $conn->prepare("INSERT INTO users (email, first_name, last_name, password_hash) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssss", $email, $first_name, $last_name, $hashed_password); } // Execute the statement if ($stmt->execute()) { $_SESSION['success'] = "Account created successfully!"; header("Location: sign-up.php"); // Redirect to the same page to show success message exit(); } else { // Check if the error is due to duplicate email if ($conn->errno === 1062) { // 1062 is the error code for duplicate entry echo "Error: An account with this email already exists."; } else { echo "Error: " . $stmt->error; } } // Close statement $stmt->close(); } // Close connection $conn->close(); ?>