PHP形式的多个if语句

时间:2016-08-29 05:24:14

标签: php mysql

我的网站上有一个“订阅”表单,我想在提交之前检查数据库中的每个条目,这里是我编码的表格,但我希望你的帮助来完成它。

表格代码

    <div class="col-lg-8 col-md-12 col-sm-12 col-xs-12">

    <h1> Subscribe </h1>  
   </div>
   <div class="col-lg-4 col-lg-offset-2 col-md-12 col-sm-12 col-xs-12"> 
    <form action="" method="post" class="inp_txt horizontal-form" enctype="multipart/form-data">

     <?php echo $result;?>
     <?php echo $error1;?>
     <?php echo $error2;?>
     <?php echo $error3;?>

     <div class="form-group">
      <label for="name"> Your Name: </label>
      <input type="text" name="name" class="form-control" value="<?=(isset($name) ? $name : "")?>" placeholder="Please enter your name" required>
     </div>
     <div class="form-group">
      <label for="phone"> Your Phone Number: </label>
      <input type="number" name="phone" class="form-control" value="<?=(isset($phone) ? $phone : "")?>" placeholder="Please enter your valid phone number" required>
     </div>
     <div class="form-group">
      <label for="email"> Your Email: </label>
      <input type="email" name="email" class="form-control" value="<?=(isset($email) ? $email : "")?>" placeholder="Please enter your valid email address">
     </div>

     <div class="form-group">
      <button type="submit" class="btn btn-info btn-md" name="subscribe" value="Subscribe me"> Subscribe me </button>
     </div>

    </form>
   </div>
 </div>
</div> 

        <?php 
    include ("include/header.php");
    include ("include/side_bar.php");
      //error_reporting(E_ALL);
      //ini_set('display_errors', 'On');
    ?>
    <?php

     $result = "";
     if(isset($_POST['subscribe'])) {
      $name = $_POST['name'];
      mysql_real_escape_string($name);
      $email = $_POST['email'];
      mysql_real_escape_string($email);
      $phone = $_POST['phone'];
      mysql_real_escape_string($phone);

      $result = "";
      $error1 = "";
      $error2 = "";
      $error3 = "";

      $check_email = "SELECT * from data where email = '$email'";
      $res_email = mysql_query($check_email) or die(mysql_error());

      $check_phone = "SELECT * from data where phone = '$phone'";
      $res_phone = mysql_query($check_phone) or die(mysql_error());

       if (empty($_POST['name']) && empty($_POST['phone']) ) {
        $error1 = "<div class='alert alert-warning'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
            <strong>Warning!</strong> Please fill all fields before submitting form!
            </div>";

           if ($res_phone >= 1) {
            $error2 = "Phone already exist";
           }

            if ($res_email >= 1) {
             $error3 = "Email already exist";
            }

            } else {


      $qry = "INSERT into `data` (name, phone, email, src) VALUES ('$name','$phone','$email','subscribed')";
      $res = mysql_query($qry) or die(mysql_error());

       if ($res == 1 ) {
        $result = "<div class='alert alert-info'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
            <strong>Success!</strong> Your information successfully saved and now you subscribed to get SMS and Emails :)
            </div>";
       } else {
        $result = "<div class='alert alert-danger'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
            <strong>Error!</strong> There was an error in saving your information, You are not subscribed to receive emails and SMS yet :(
            </div>";
       }
      }
     } else {
       // do noting
     }

    ;?>

一旦我保存了这些代码,一旦执行了第一个IF语句,其他三个带有一个else语句的IF语句仍未执行。

3 个答案:

答案 0 :(得分:0)

试试这样 `     

 $result = "";
 if(isset($_POST['subscribe'])) {
  $name = $_POST['name'];
  mysql_real_escape_string($name);
  $email = $_POST['email'];
  mysql_real_escape_string($email);
  $phone = $_POST['phone'];
  mysql_real_escape_string($phone);

  $result = "";
  $error1 = "";
  $error2 = "";
  $error3 = "";
  $error = false;

  $check_email = "SELECT * from data where email = '$email'";
  $res_email = mysql_query($check_email) or die(mysql_error());

  $check_phone = "SELECT * from data where phone = '$phone'";
  $res_phone = mysql_query($check_phone) or die(mysql_error());

    if (empty($_POST['name']) && empty($_POST['phone']) ) {
    $error1 = "<div class='alert alert-warning'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
        <strong>Warning!</strong> Please fill all fields before submitting form!
        </div>";
        $error = true;
    }

    if(!$error && $res_phone >= 1) {
        $error2 = "Phone already exist";
        $error = true;
    }

    if(!$error && $res_email >= 1) {
        $error3 = "Email already exist";
        $error = true;
    }

    if(!$error){
        $qry = "INSERT into `data` (name, phone, email, src) VALUES ('$name','$phone','$email','subscribed')";
        $res = mysql_query($qry) or die(mysql_error());

       if ($res == 1 ) {
            $result = "<div class='alert alert-info'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                <strong>Success!</strong> Your information successfully saved and now you subscribed to get SMS and Emails :)
                </div>";
            } else {
            $result = "<div class='alert alert-danger'>
                <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                <strong>Error!</strong> There was an error in saving your information, You are not subscribed to receive emails and SMS yet :(
                </div>";
            }
        }
 } else {
   // do noting
 }

;?>`

答案 1 :(得分:0)

对所有字段都这样,因为如果查询成功运行或在数据库中找到任何记录,mysql_num_rows()函数将给出大于0的整数值

$check_phone = "SELECT * from data where phone = '$phone'";
$res_phone = mysql_query($check_phone) or die(mysql_error());
$res_phone_count = mysql_num_rows($res_phone);

if ($res_phone_count > 0)
{
     $error2 = "Phone already exist";
}

答案 2 :(得分:0)

  

警告mysql_query,mysql_fetch_array,mysql_connect等..扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。   相反,应该使用MySQLi或PDO_MySQL扩展。

试试这个

    //db connection

     global $conn;

        $servername = "localhost";  //host name

        $username = "username"; //username

        $password = "password"; //password

        $mysql_database = "dbname"; //database name

    //mysqli prepared statement 

        $conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

       mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");



    $result = "";
     if(isset($_POST['subscribe'])) {
      $name = $_POST['name'];
      mysqli_real_escape_string($name);
      $email = $_POST['email'];
      mysqli_real_escape_string($email);
      $phone = $_POST['phone'];
      mysqli_real_escape_string($phone);

      $result = "";
      $error1 = "";
      $error2 = "";
      $error3 = "";
      $error = false;

      //$check_email = "SELECT * from data where email = '$email'";
     // $res_email = mysql_query($check_email) or die(mysql_error());

      $stmt = $conn->prepare("SELECT * from data where email =?");

                    $stmt->bind_param('s',$email);

                    The argument may be one of four types:

                    i - integer
                    d - double
                    s - string
                    b - BLOB
                    //change it by respectively 

                    $stmt->execute();

                    $get_result =$stmt->get_result();

                    $row_count_email= $get_result->num_rows;

                    //$row_count= $stmt->affected_rows;

                    $stmt->close();


      //$check_phone = "SELECT * from data where phone = '$phone'";
      //$res_phone = mysql_query($check_phone) or die(mysql_error());

      $stmt1 = $conn->prepare("SELECT * from data where phone =?");

                    $stmt1->bind_param('s',$phone); //here data type change based on your db design 

                    The argument may be one of four types:

                    i - integer
                    d - double
                    s - string
                    b - BLOB
                    //change it by respectively 

                    $stmt1->execute();

                    $get_result1 =$stmt1->get_result();

                    $row_count_phone= $get_result1->num_rows;

                    //$row_count= $stmt1->affected_rows;

                    $stmt1->close();

        if (empty($_POST['name']) && empty($_POST['phone']) ) {
        $error1 = "<div class='alert alert-warning'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
            <strong>Warning!</strong> Please fill all fields before submitting form!
            </div>";
            $error = true;
        }

        if($row_count_phone >= 1) {
            $error2 = "Phone already exist";
            $error = true;
        }

        if($row_count_email >= 1) {
            $error3 = "Email already exist";
            $error = true;
        }

        if(!$error){

                //$qry = "INSERT into `data` (name, phone, email, src) VALUES ('$name','$phone','$email','subscribed')";
                //$res = mysql_query($qry) or die(mysql_error());

                $stmt2 = $conn->prepare("INSERT into `data` (name, phone, email, src) VALUES (?,?,?,?)");

                    $stmt2->bind_param('ssss',$name,$phone,$email,'subscribed');

                    The argument may be one of four types:

                    i - integer
                    d - double
                    s - string
                    b - BLOB
                    //change it by respectively 

                    $stmt2->execute();

                    //$get_result2 =$stmt2->get_result();

                    //$row_count= $get_result2->num_rows;

                    $row_count= $stmt2->affected_rows;

                    $stmt2->close();
                     $conn2->close();

               if ($row_count >=1 ) {
                    $result = "<div class='alert alert-info'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <strong>Success!</strong> Your information successfully saved and now you subscribed to get SMS and Emails :)
                        </div>";
                    } else {
                    $result = "<div class='alert alert-danger'>
                        <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
                        <strong>Error!</strong> There was an error in saving your information, You are not subscribed to receive emails and SMS yet :(
                        </div>";
                    }
            }
     } 

     ?>

并使用if条件为错误并导致形式否则抛出错误

<?php if($result){ echo $result; } ?>
 <?php if($error1){ echo $error1; } ?>