i have this code bellow:
if($name !=null && $email !=null && $password !=null && $gender !=null) { if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailError = "you should write a correct email"; } if(strlen($password) < 6) { $passwordError = "password should consist from 6 charachters or more"; } $result = mysqli_query($conn, $sql); if($result) { header("location:index.php"); }else { echo "Error"; } } else { $msg = "you should fill all the form"; } }
it doesn’t show any error when the email is not correct or the password is less than 6, and inserts data normally, I need to show for example two error messages if both password and email are not correct, how can I do it?
Advertisement
Answer
Amend your code so that
- it will NOT execute the db query if there is/are error(s)
- Display the errors if they exist.
<?php //initialize the error messages to be blank $emailError =""; $passwordError=""; if($name !=null && $email !=null && $password !=null && $gender !=null) { if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $emailError = " you should enter a correct email ;"; } if(strlen($password) < 6) { $passwordError = " password should consist of 6 characters or more ;"; } if ($emailError =="" && $passwordError=="") { $result = mysqli_query($conn, $sql); if($result) { header("location:index.php"); } else { echo "Error"; } } else { echo "You have the following error(s) :"; if ($emailError !=""){ echo $emailError; } if ($passwordError !=""){ echo $passwordError; } } } else { // $msg = "you should fill all the form"; echo "you should enter all the required data in the form"; } ?>