0
我已经实现了一个用户注册表单,并且我需要检查ABN字段是否正好有11个字符。我为此写了一个if else语句。但据说即使有人输入了11个字符,也会弹出错误信息。 if-else语句可能有问题。虽然我是编程新手,但很难发现错误。我已经更改了if-else的展示位置,但我无法修复它。任何形式的帮助将非常感激。我会把我的编码放在下面。谢谢!使用strlen()函数检查字符串是否有11个字符
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语句应该是'如果(strlen的($ ABN)!= 11) { echo'ABN应该是11个字符。'; }' – JYoThI
检查$ abn变量中存储的值。我看不到它在哪里被初始化。我怀疑它可能没有初始化。 – davidwebster48
感谢堆@JYoThI它的工作。从字面上拯救我的生命! <3 –