【发布时间】:2017-08-31 03:36:22
【问题描述】:
我已经实现了一个用户注册表单,我需要检查 ABN 字段是否正好有 11 个字符。我为此写了一个 if else 语句。但据此,即使有人输入了 11 个字符,它也会弹出错误消息。 if-else 语句可能有问题。虽然我是编程新手,但很难找到错误。我也更改了 if-else 展示位置,但我无法修复它。任何形式的帮助将不胜感激。我将把我的编码放在下面。谢谢!
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.';}
}
}
}
}
}
【问题讨论】:
-
你的if语句应该是
if(strlen($abn)!=11) { echo 'ABN Should be 11 characters.'; } -
检查 $abn 变量中存储的值。我看不到它在哪里初始化。我怀疑它可能没有初始化。
-
谢谢@JYoThI 成功了。从字面上救我的命!
-
很高兴它帮助你:) @RyanOscar
标签: php if-statement strlen