在PHP中通过电子邮件验证一个条件和多个条件

时间:2019-02-02 11:31:48

标签: php validation if-statement nested-loops

我将尝试将电子邮件验证包含多个案例。 First(if):email是必需的。 Second(if):电子邮件格式无效。 第三(其他):电子邮件存在(活动用户) 第四(其他):电子邮件处于批准状态(管理员尚未批准为帐户,因此其       仍在请求表中) 第五(其他):一切顺利,将数据发布到数据库中。

我尝试通过if-else语句解决这个问题,但是在最后两种情况下,我没有任何条件,只需要通过该验证即可。我还尝试了switch语句,但是在第一种情况下并没有采用这种方法: >

这是我的验证:

scrape

3 个答案:

答案 0 :(得分:0)

以下是执行此操作的方法:

为每个错误声明一些随机变量 通过使用多个if条件,首先检查它是否为空。第二次检查是否匹配正确的格式,第三次检查值是否存在db

我正在为此显示示例代码

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 :(得分:0)

在第1行中,您选中if(empty($_POST['email'])),  因此,如果为空,则应该返回错误,而是尝试在第3行中保存空值,依此类推。

答案 2 :(得分:0)

好吧,我把$ error的条件和现在的正常工作了。请记住,你必须把$存在=“”;在开始。 这是我的代码:

   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;
              }
            }

        }
相关问题