I'm not sure what I'm doing wrong. I'm trying to create a simple form which will not upload to the MySQL DB if the fields are empty. I also have some basic validation in the form. However, when I click 'save' the blank form is uploaded to the DB and I also do not get the error messages per field as I have highlighted them.
I would appreciate suggestion on where I err.
Here's my PHP Code:
<?php
$firstNameError = $lastNameError = $idNumberError = $mobileNumberError = $emailError = $birthDateError = $languageTypeError = $interestError = "";
$firstName = $lastName = $idNumber = $mobileNumber = $email = $birthDate = $languageType = $interest = "";
if(isset($_POST['submit'])) {
include 'dbconnect.php';
try {
$sql = "INSERT INTO members SET
firstName = :firstName,
lastName = :lastName,
idNumber = :idNumber,
mobileNumber = :mobileNumber,
email = :email,
birthDate = :birthDate,
languageType = :languageType,
interest = :interest,
created = :created";
$stmt = $conn->prepare($sql);
if (empty($_POST["firstName"])) {
$firstNameError = "First Name is Required Please";
} else {
$firstName = clean_data($_POST["firstName"]);
if (!preg_match("/^[a-zA-Z ]*$/",$firstName)) {
$firstNameError = "Only letters and white space allowed";
}
}
if (empty($_POST["lastName"])) {
$lastNameError = "Last Name is Required Please";
} else {
$lastName = clean_data($_POST["lastName"]);
if (!preg_match("/^[a-zA-Z ]*$/",$lastName)) {
$lastNameError = "Only letters and white space allowed";
}
}
if (empty($_POST["idNumber"])) {
$idNumberError = "ID Number is Required Please";
} else {
$idNumber = clean_data($_POST["idNumber"]);
if (!preg_match("/^[0-9]{13}$/",$idNumber)) {
$idNumberError = "ID must be exactly 13 digits and no white spaces or other characters";
}
}
if (empty($_POST["mobileNumber"])) {
$mobileNumberError = "Mobile Nr is Required Please";
} else {
$mobileNumber = clean_data($_POST["mobileNumber"]);
if (!preg_match("/^[0-9]{10}$/",$mobileNumber)) {
$mobileNumberError = "Your phone nr must be exactly 10 digits and no white spaces or other characters";
}
}
if (empty($_POST["email"])) {
$emailError = "Email is Required Please";
} else {
$email = clean_data($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailError = "Please enter a valid email address";
}
}
if (empty($_POST["birthDate"])) {
$birthDateError = "Birth Date is Required Please";
} else {
$birthDate = clean_data($_POST["birthDate"]);
list($dd,$mm,$yyyy) = explode('-',$birthDate);
if (!checkdate($yyyy,$mm,$dd)) {
$birthDateError = "Please use the format YYYY-MM-DD";
}
}
if (empty($_POST["languageType"])) {
$languageTypeError = "Languge Type is Required Please";
} else {
$languageType = clean_data($_POST["languageType"]);
}
if (empty($_POST["interest"])) {
$interestError = "Interest is Required Please";
} else {
$interest = clean_data($_POST["interest"]);
}
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':idNumber', $idNumber);
$stmt->bindParam(':mobileNumber', $mobileNumber);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':birthDate', $birthDate);
$stmt->bindParam(':languageType', $languageType);
$stmt->bindParam(':interest', $interest);
$created = date('Y-m-d H:i:s');
$stmt->bindParam(':created', $created);
if($stmt->execute()){
echo "<div class='alert alert-success'>Member was saved.</div>";
} else {
echo "<div class='alert alert-danger'>Unable to save this member.</div>";
}
}
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
function clean_data($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
And here is my HTML Code:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>First Name: *</td>
<td><input type='text' name='firstName' class='form-control' /></td>
</tr>
<tr>
<td>Last Name: *</td>
<td><input type='text' name='lastName' class='form-control' /></td>
</tr>
<tr>
<td>ID Number: *</td>
<td><input type='text' name='idNumber' class='form-control' /></td>
</tr>
<tr>
<td>Mobile Number: *</td>
<td><input type='text' name='mobileNumber' class='form-control' /></td>
</tr>
<tr>
<td>Email: *</td>
<td><input type='text' name='email' class='form-control' /></td>
</tr>
<tr>
<td>Birth Date</td>
<td><input type='text' name='birthDate' class='form-control' /></td>
</tr>
<tr>
<td>Language</td>
<td>
<select class="form-control" name="languageType">
<option>Select One...</option>
<option>Afrikaans</option>
<option>English</option>
<option>Zulu</option>
<option>Xhosa</option>
<option>Venda</option>
<option>French</option>
</select>
</td>
</tr>
<tr>
<td>Interest</td>
<td>
<select class="form-control" name="interest">
<option>Select One...</option>
<option>Golf</option>
<option>Rugby</option>
<option>Tennis</option>
<option>Cricket</option>
<option>Swimming</option>
<option>Hiking</option>
<option>Surfing</option>
<option>Movies</option>
<option>Swords</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' name='submit' value='Save' class='btn btn-primary' />
<a href='index.php' class='btn btn-danger'>Back to view members</a>
</td>
</tr>
</table>
</form>