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
As for your first question, don't check for $var
, check for !empty($var)
.
Second question: I am not sure I understand what you are trying to do. But don't you mean $row
instead of $rows
when you are displaying something? Also, what if you have more than one search hit? What you want is:
while($row=mysql_fetch_assoc($result)){
echo $row['Firstname'], ' ', $row['LastName'];
}
instead of your if-else construction.
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 '<p>The First name is empty or has illegal characters! To edit please go the link Display Data Information</p>';
$fname = "";
}