I have created a small registration sticky form. Everything is working fine, but if I input any wrong value,like numbers in name, letters in age or even wrong email format, then
your variables like $fname $lname $gender $age $email $course
are put in if condition after if condition for isset($_POST['register'])
. Now even if your validation will work, data will still be entered in database. because you have put condition
if($fname&&$lname&&$gender&&$age&&$email&&$course)
Now control will enter that block when you have even a single value in all of those variable. What must be happening is, that you put wrong values, those are getting validated, message will be shown, but when first if block finishes, as $_POST variables still have SOME value, regardless of them being invalid, second if block will be entered and query will be fired.
What you can do is, where ever you are echoing error message, blank out that respective variable. something like this:
if (preg_match("/[a-zA-Z ]+$/", $_POST['fname'])) {
$fname = trim($_POST['fname']);
}
else
{
echo 'The First name is empty or has illegal characters! To edit please go the link Display Data Information
';
$fname = "";
}