Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP格式的多if语句_Php_Mysql - Fatal编程技术网

PHP格式的多if语句

PHP格式的多if语句,php,mysql,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-

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

表格代码

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

    ;?>

订阅
你的名字:
像这样试试
`
$result=”“;
如果(isset($_POST['subscribe'])){
$name=$_POST['name'];
mysql\u real\u escape\u字符串($name);
$email=$_POST['email'];
mysql\u real\u escape\u字符串($email);
$phone=$_POST['phone'];
mysql\u real\u escape\u字符串($phone);
$result=“”;
$error1=“”;
$error2=“”;
$error3=“”;
$error=false;
$check_email=“从数据中选择*,其中email='$email';
$res_email=mysql_query($check_email)或die(mysql_error());
$check_phone=“从数据中选择*,其中phone='$phone';
$res_phone=mysql_query($check_phone)或die(mysql_error());
if(空($\u POST['name'])和空($\u POST['phone'])){
$error1=”
警告!提交表格前请填写所有字段!
";
$error=true;
}
如果(!$error&&$res\u phone>=1){
$error2=“电话已存在”;
$error=true;
}
如果(!$error&&$res\u email>=1){
$error3=“电子邮件已存在”;
$error=true;
}
如果(!$error){
$qry=“在`数据'(姓名、电话、电子邮件、src)中插入值('$name','$phone','$email','subscribed')”;
$res=mysql\u query($qry)或die(mysql\u error());
如果($res==1){
$result=”
成功!您的信息已成功保存,现在您已订阅短信和电子邮件:)
";
}否则{
$result=”
错误!保存信息时出错,您尚未订阅接收电子邮件和短信:(
";
}
}
}否则{
//什么也不做
}
;?>`

对于所有字段,请遵循此操作,因为如果查询成功运行或在数据库中找到任何记录,mysql\u num\u rows()函数将为您提供大于0的整数值

$check_phone=“从数据中选择*,其中phone='$phone';
$res_phone=mysql_query($check_phone)或die(mysql_error());
$res\u phone\u count=mysql\u num\u行($res\u phone);
如果($res\u phone\u count>0)
{
$error2=“电话已存在”;
}
警告:mysql_查询、mysql_获取_数组、mysql_连接等扩展在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>";
                    }
            }
     } 

     ?>
//数据库连接
全球$conn;
$servername=“localhost”;//主机名
$username=“username”;//用户名
$password=“password”;//密码
$mysql\u database=“dbname”;//数据库名称
//mysqli编写的声明
$conn=mysqli_connect($servername、$username、$password)或die(“连接失败:”.mysqli_connect_error());
mysqli_选择db($conn,$mysql_数据库)或die(“Opps一些事情出错”);
$result=“”;
如果(isset($_POST['subscribe'])){
$name=$_POST['name'];
mysqli_real_escape_字符串($name);
$email=$_POST['email'];
mysqli_real_escape_字符串($email);
$phone=$_POST['phone'];
mysqli_real_escape_string($phone);
$result=“”;
$error1=“”;
$error2=“”;
$error3=“”;
$error=false;
//$check_email=“从数据中选择*,其中email='$email';
//$res_email=mysql_query($check_email)或die(mysql_error());
$stmt=$conn->prepare(“从电子邮件=?”的数据中选择*”;
$stmt->bind_param('s',$email);
参数可以是四种类型之一:
i-整数
双d
s字串
b团
//分别更改为
$stmt->execute();
$get_result=$stmt->get_result();
$row\u count\u email=$get\u result->num\u rows;
//$row\u count=$stmt->受影响的\u行;
$stmt->close();
//$check_phone=“从数据中选择*,其中phone='$phone';
//$res_phone=mysql_query($check_phone)或die(mysql_error());
$stmt1=$conn->prepare(“从手机=?”的数据中选择*”;
$stmt1->bind_param('s',$phone);//此处根据您的数据库设计更改数据类型
参数可以是四种类型之一:
i-整数
双d
s字串
b团
//分别更改为
$stmt1->execute();
$get_result1=$stmt1->get_result();
$row\u count\u phone=$get\u result1->num\u rows;
//$row\u count=$stmt1->受影响的\u行;
$stmt1->close();
if(空($\u POST['name'])和空($\u POST['phone'])){
$error1=”
警告!提交表格前请填写所有字段!
";
$error=true;
}
如果($row\u count\u phone>=1){
$error2=“电话已存在”;
$error=true;
}
如果($row\u count\u email>=1){
$error3=“电子邮件已存在”;
$error=true;
}
如果(!$error){
//$qry=“在`数据'(姓名、电话、电子邮件、src)中插入值('$name','$phone','$email','subscribed')”;
//$res=mysql\u query($qry)或die(mysql\u error());
$stmt2=$conn->prepare(“插入到`数据'(姓名、电话、电子邮件、src)值(?,?,?)”;
$stmt2->bind_param('ssss',$name,$phone,$email,'subscribed');
参数可以是四种类型之一:
i-整数
    //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>";
                    }
            }
     } 

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