代码之家  ›  专栏  ›  技术社区  ›  Sagar Parikh

电子邮件验证PHP中的一个条件和多个else语句

  •  0
  • Sagar Parikh  · 技术社区  · 7 年前

    我会尝试将电子邮件验证放在一个以上的案例中。 第二(如果):电子邮件格式无效。 第三(否则):电子邮件存在(活动用户) 第四(其他):电子邮件处于批准状态(管理员尚未批准帐户,因此其 仍在请求表中) 第五(其他):一切顺利,将数据发布到数据库中。

    我尝试用if-else语句来解决这个问题,但在最后两种情况下,我没有任何条件,它只需要通过验证即可。我也尝试使用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;
                 }
               }
          }   
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   harjot singh chana    7 年前

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

    为每个错误声明一些随机变量 通过使用多个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
    }
    
        2
  •  0
  •   Brian Tompsett - 汤莱恩 andrewwong97    7 年前

    在第一行,你检查一下 if(empty($_POST['email'])) , 所以,如果它是空的,您应该返回error,而不是尝试在第3行保存空值,依此类推。

        3
  •  0
  •   Sagar Parikh    7 年前

    这是我的密码:

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