当前位置:  开发笔记 > 编程语言 > 正文

为什么我的IF声明不能显示我想要的结果?

如何解决《为什么我的IF声明不能显示我想要的结果?》经验,为你挑选了1个好方法。

我的customer_profile表中有两列,"birthdate"和"gender".我想要检测用户是否已完成他们的个人资料.但是,通过使用以下条件,我无法检查客户是否已完成其个人资料.它只返回"配置文件完成".

我试过改变条件"&& to ||" 并且"|| to &&"和all to"&&"但它会返回意外的结果.

有这么多条件的原因是因为有时客户没有填写数据或将其留空或在性别字段或生日字段中键入"0".如果只有两个条件,我就不必面对这个问题了.

下面是我比较这两列的代码.我要检查的是客户数据是否为空; 这两列上为NULL或0.

$sql_stmt = "SELECT customer_id, first_name, email, phone, birthdate, gender FROM customers
                        WHERE customer_id = 1 LIMIT 1";
$sql = mysqli_query($conn, $sql_stmt) or die(mysqli_error($conn));
$row = mysqli_fetch_array( $sql );
extract($row);

if(($birthdate == '' && $birthdate == NULL && $birthdate == '0000-00-00') || ($gender == '' && $gender == 0 && $gender == NULL)){
     echo "Profile Not Complete";
} else { 
     echo "Profile Complete"; 
}

Marcos Pérez.. 7

这应该是这样的:

if(($birthdate == '' || $birthdate == NULL || $birthdate == '0000-00-00') || ($gender == '' || $gender == 0 || $gender == NULL)){
     echo "Profile Not Complete";
} else { 
     echo "Profile Complete"; 
}

这意味着:

如果组$ birthdate为空或null或0000000,或者性别为空或零或空...

编辑

我建议使用这种条件方式:

if(empty($birthdate) || empty($gender)){
     echo "Profile Not Complete";
} else { 
     echo "Profile Complete"; 
}

了解更多empty():

http://php.net/manual/en/function.empty.php



1> Marcos Pérez..:

这应该是这样的:

if(($birthdate == '' || $birthdate == NULL || $birthdate == '0000-00-00') || ($gender == '' || $gender == 0 || $gender == NULL)){
     echo "Profile Not Complete";
} else { 
     echo "Profile Complete"; 
}

这意味着:

如果组$ birthdate为空或null或0000000,或者性别为空或零或空...

编辑

我建议使用这种条件方式:

if(empty($birthdate) || empty($gender)){
     echo "Profile Not Complete";
} else { 
     echo "Profile Complete"; 
}

了解更多empty():

http://php.net/manual/en/function.empty.php

推荐阅读
赛亚兔备_393
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有