My database validation is working; the 'User' table will not update if there is already a registered user. However, my $error_message variable will not display the the error message string. Here is my code:
HTML/PHP:
// Collect and validate user inputs
if($_SERVER["REQUEST_METHOD"] == "POST") {
session_start();
$forename = trim(filter_input(INPUT_POST,"user_forename",FILTER_SANITIZE_STRING));
$surname = trim(filter_input(INPUT_POST,"user_surname",FILTER_SANITIZE_STRING));
$gender = trim(filter_input(INPUT_POST,"user_gender",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"user_email",FILTER_SANITIZE_EMAIL));
$password = trim(filter_input(INPUT_POST,"user_password"));
$city = trim(filter_input(INPUT_POST,"user_city"));
$team = trim(filter_input(INPUT_POST,"user_team",FILTER_SANITIZE_STRING));
$bio = trim(filter_input(INPUT_POST,"user_bio",FILTER_SANITIZE_SPECIAL_CHARS));
$human = trim(filter_input(INPUT_POST,"user_human",FILTER_SANITIZE_STRING));
$userExist = mysql_query("SELECT * FROM User WHERE U_Email='$email'");
if($forename == "" || $surname == "" || $email == "" || $password == ""
|| $city == "" || $team == "" || $bio == "" || $human == "") {
$error_message = "Please fill in all form fields";
}
if (!isset($error_message) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error_message = "$email is a not a valid email address";
}
if (!isset($error_message) && (mysql_num_rows($userExist) > 0)) {
$error_message = "$email is already taken!";
}
if(!isset($error_message)) {
$sql = $db->query("INSERT INTO User (U_Forename, U_Surname, U_Gender, U_Email, U_Password, U_City, U_Team, U_Biography)
VALUES('{$forename}', '{$surname}', '{$gender}', '{$email}', '{$password}', '{$city}', '{$team}', '{$bio}')");
// header('Location: index.php');
}
}
<div class="wrapper">
<h1>Register, it's free!</h1>
<div>
<?php
if (isset($error_message)) {
echo "<h2>".$error_message."</h2>";
}
?>
</div>
No error message will be displayed after the form has been submitted. Moreover, I am not receiving any PHP errors so I am not sure what is the problem.
Any suggestions would be great.
Thanks, James.