Reputation: 279
I've implemented a user registration form and I need to check if ABN field has exactly 11 characters. I wrote an if else statement for that. But according to that even though someone inputs 11 characters it pops up the error message. Probably there is something wrong with the if-else statement. Though I'm new to programming it's really hard to find the error. I've changed the if-else placements too but I couldn't fix it. Any sort of help would be really appreciated. I'll put my coding down below. Thank you!
if($_POST){
//validate email
if(empty($_POST['email']) || empty($_POST['password']) || empty($_POST['confirm']) || empty($_POST['firstname']) || empty($_POST['lastname']) ||empty($_POST['companyname']) || empty($_POST['companyphone'])||empty($_POST['abn'])||empty($_POST['type'])||empty($_POST['position'])||empty($_POST['phone']) || empty($_POST['address1']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['country']) || empty($_POST['postcode'] )){
$errors[] = 'You must fill out required field.';
} else {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$errors[] = 'You must enter a valid email.';
}else {
$query = "SELECT * FROM users WHERE email=?";
$stmt = $db->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = mysqli_fetch_assoc($result);
$stmt->execute();
$stmt->store_result();
$userCount = $stmt->num_rows();
// Check if the email already exists in database.
if($userCount > 0){
$errors[] = 'That email already exists.';
} else {
//check if password is not less than 6 characters
if (strlen($password) < 6) {
$errors[] = 'Password must be at least 6 characters.';
}else {
// check if passwords match
if($password !=$confirm){
$errors[] = 'The passwords do not match.';
}
else{
if(strlen($abn)< 11)
{$errors[] = 'ABN Should be 11 characters.';}
}
}
}
}
}
Upvotes: 0
Views: 137
Reputation: 12085
As you stated that your strlength should be exactly 11 So Your if statement should be like this
if(strlen($abn)!=11) { $errors[] ='ABN Should be 11 characters.'; }
Upvotes: 1