PHP 中的电子邮件验证一个条件和多个 else 语句
email validation one condition & more than one else statement in PHP
我会尝试将电子邮件验证放在不止一种情况下。
第一(如果):需要电子邮件。
第二(如果):电子邮件格式无效。
第三(其他):电子邮件存在(活跃用户)
第四(其他):电子邮件正在批准中(管理员尚未批准帐户,因此其
仍在申请中 table)
Fifth(else):一切顺利,post数据进入数据库
我尝试通过 if-else 语句解决这个问题,但在最后两种情况下我没有任何条件,它只需要通过 validation.I 也尝试 switch 语句,但它不是在第一种情况之后:
这是我的验证:
if(empty($_POST['email']))
{
$email=$_POST['email'];
$emailError="Email is required";
$error=true;
}
else
{
$email=$_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailError = "Invalid email format";
$error=true;
}
else
{
$sql="SELECT * FROM user WHERE email='$email'";
$res=mysqli_query($db,$sql);
if (mysqli_num_rows($res) > 0)
{
$emailError = "Email already exists";
$error=true;
}
}
else
{
$sl="SELECT * FROM request WHERE email='$email'";
$ress=mysqli_query($db,$sl);
if (mysqli_num_rows($ress) > 0)
{
$emailError = "Your Accout is in process";
$error=true;
}
}
}
以下是执行此操作的方法:
为每个错误声明一些随机变量
通过使用多个 if 条件,首先检查它是否为空。第二次检查它是否匹配正确的格式,第三次检查数据库中是否存在值
我正在显示此示例代码
if(empty($_POST['email'])){
$emptyMail = 'Please provide the Email';
}else{
$notEmpty= 1;
}
if(!){ //check the preg match
wrongFromat = 'Please enter the Mail in correct format';
}else{
$correctFormat = 1;
}
if($notEmpty= 1 && $correctFormat = 1){ //check the database
//if it exist in db
$exist = 0;
}elseif($notEmpty= 0 || $correctFormat = 0 || $notEmpty= 1 || $correctFormat = 1){
$exist = 0;
}else{
$exist = 1;
}
if($notEmpty= 1 && $correctFormat = 1 $exist = 1){
//insert in database and send the status pending to the user
}
在第 1 行中检查 if(empty($_POST['email']))
,
所以如果它是空的,你应该 return 错误,而不是你尝试在第 3 行保存空值等等。
好的,我为 $error 添加了条件,现在可以正常工作了。请记住,您必须输入 $exist="";在开始。
这是我的代码:
if(empty($_POST['email']))
{
$email=$_POST['email'];
$emailError="Email is required";
$error=true;
}
else {
$email=$_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailError = "Invalid email format";
$error=true;
}
else
{
$sql="SELECT * FROM user WHERE email='$email'";
$res=mysqli_query($db,$sql);
if (mysqli_num_rows($res) > 0)
{
$emailError = "Email already exists";
$error=true;
$exist=1;
}
}
if($exist!=1)
{
$sl="SELECT * FROM request WHERE email='$email'";
$ress=mysqli_query($db,$sl);
if (mysqli_num_rows($ress) > 0)
{
$emailError = "Your Accout is in process";
$error=true;
}
}
}
我会尝试将电子邮件验证放在不止一种情况下。 第一(如果):需要电子邮件。 第二(如果):电子邮件格式无效。 第三(其他):电子邮件存在(活跃用户) 第四(其他):电子邮件正在批准中(管理员尚未批准帐户,因此其 仍在申请中 table) Fifth(else):一切顺利,post数据进入数据库
我尝试通过 if-else 语句解决这个问题,但在最后两种情况下我没有任何条件,它只需要通过 validation.I 也尝试 switch 语句,但它不是在第一种情况之后:
这是我的验证:
if(empty($_POST['email']))
{
$email=$_POST['email'];
$emailError="Email is required";
$error=true;
}
else
{
$email=$_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailError = "Invalid email format";
$error=true;
}
else
{
$sql="SELECT * FROM user WHERE email='$email'";
$res=mysqli_query($db,$sql);
if (mysqli_num_rows($res) > 0)
{
$emailError = "Email already exists";
$error=true;
}
}
else
{
$sl="SELECT * FROM request WHERE email='$email'";
$ress=mysqli_query($db,$sl);
if (mysqli_num_rows($ress) > 0)
{
$emailError = "Your Accout is in process";
$error=true;
}
}
}
以下是执行此操作的方法:
为每个错误声明一些随机变量 通过使用多个 if 条件,首先检查它是否为空。第二次检查它是否匹配正确的格式,第三次检查数据库中是否存在值
我正在显示此示例代码
if(empty($_POST['email'])){
$emptyMail = 'Please provide the Email';
}else{
$notEmpty= 1;
}
if(!){ //check the preg match
wrongFromat = 'Please enter the Mail in correct format';
}else{
$correctFormat = 1;
}
if($notEmpty= 1 && $correctFormat = 1){ //check the database
//if it exist in db
$exist = 0;
}elseif($notEmpty= 0 || $correctFormat = 0 || $notEmpty= 1 || $correctFormat = 1){
$exist = 0;
}else{
$exist = 1;
}
if($notEmpty= 1 && $correctFormat = 1 $exist = 1){
//insert in database and send the status pending to the user
}
在第 1 行中检查 if(empty($_POST['email']))
,
所以如果它是空的,你应该 return 错误,而不是你尝试在第 3 行保存空值等等。
好的,我为 $error 添加了条件,现在可以正常工作了。请记住,您必须输入 $exist="";在开始。 这是我的代码:
if(empty($_POST['email']))
{
$email=$_POST['email'];
$emailError="Email is required";
$error=true;
}
else {
$email=$_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailError = "Invalid email format";
$error=true;
}
else
{
$sql="SELECT * FROM user WHERE email='$email'";
$res=mysqli_query($db,$sql);
if (mysqli_num_rows($res) > 0)
{
$emailError = "Email already exists";
$error=true;
$exist=1;
}
}
if($exist!=1)
{
$sl="SELECT * FROM request WHERE email='$email'";
$ress=mysqli_query($db,$sl);
if (mysqli_num_rows($ress) > 0)
{
$emailError = "Your Accout is in process";
$error=true;
}
}
}