php和mysql过滤和搜索

时间:2018-05-26 01:27:53

标签: php mysql

我尝试使用各种过滤标准/参数创建过滤器和搜索功能。 但我收到这些错误: 警告:implode():传递的参数无效 警告:mysqli_fetch_assoc()期望参数1为mysqli_result。 首先,我尝试清理广告过滤输入数据,然后创建过滤器以进行更简化的搜索。因此,当应用一个或多个过滤器时,查询结果会更准确。

    <?php
    session_start();
    include_once('server.php');

    $error = false;
    $int = 100;


   if(isset($_POST['btn-login'])){
   $firstname = $_POST['firstname'];
   $firstname = trim($firstname);
   $firstname = trim($_POST['firstname']);
   $firstname = htmlspecialchars(strip_tags($firstname));

   $lastname = $_POST['lastname'];
   $lastname = trim($lastname);
   $lastname = trim($_POST['lastname']);
   $lastname = htmlspecialchars(strip_tags($lastname));

    $phonenumber = $_POST['phonenumber'];
    $phonenumber = trim($phonenumber);
    $phonenumber = trim($_POST['phonenumber']);
    $phonenumber = htmlspecialchars(strip_tags($phonenumber));



    if(empty($firstname)){
    $error = true;
    $errorFirstname = '<h6>Please input first name or last name.</h6>';
      }

    if(empty($phonenumber)){
    $error = true;
    $errorPhonenumber = '<h6>Please input phone number.</h6>';
     }

     $firstname = $_POST['firstname'];
     if(!preg_match("/^[a-zA-Z]*$/",$firstname)){
     $error = true;
     $errorFirstname = '<h6>Please input letters only.</h6>';
    }

    $lastname = $_POST['lastname'];
    if(!preg_match("/^[a-zA-Z]*$/",$lastname)){
    $error = true;
    $errorLastname = '<h6>Please input letters only.</h6>';
     }



    /*if($firstname = $int) {
    $error = true;
    $errorFirstname = 'Invalid entry! Letters only.';
     }*/

      /*  if (!filter_var($int, FILTER_VALIDATE_INT) === false) {
     echo("Integer is valid");    //work in progress
         } else {
      echo("Integer is not valid");
        }  */

       if(is_numeric($phonenumber)){
        $msg = 'This is a valid number.';
       }
       else{
       $error = true;
       $errorPhonenumber = '<h6>Invalid entry, numbers only.<h6>';
       }



      /*  if($lastname = $int) {
       $error = true;
       $errorLastname = 'Invalid entry! Letters only.';
     }  */


      if(empty($phonenumber)){
      $error = true;
      $errorPhonenumber = '<h6>Please Input Phone Number.</h6>';        
      }elseif(strlen($phonenumber)< 9){
      $error = true;
      $errorPhonenumber = '<h6> number must be at least 9 chars.';
      } 


     if(empty($phonenumber)){
     $error = true;
     $errorPhonenumber = '<h6>Please Input Phone Number.</h6>';     
     }elseif(strlen($phonenumber)> 11){
     $error = true;
     $errorPhonenumber = 'number must not exceed 11 characters.';
      } 





         if(!$error) {
         //search data if no errors
         $sql = "select * from subscribers";
         $conditions = array();

         if(!empty($firstname)){
         $conditions = "name='$firstname'";
         }

        if(!empty($lastname)){
        $conditions = "lastname='$lastname'";
        }

        if(!empty($sex)){
        $conditions = "gender='$gender'";
        }


        if(count($conditions)>0){
        $sql.="WHERE" .implode('AND',$conditions);
        }


       $result = mysqli_query($conn, $sql);


       while ($subscriber=mysqli_fetch_assoc($result)){

       $msg1 = $subscriber['firstname'];
       $msg2 = $subscriber['lastname'];
       $msg3 = $subscriber['phonenumber'];

       }        

       }
       /*(mysqli_query($conn, $sql)){
         }else{
         echo 'Error '.mysqli_error($conn);
       }    */

       }

       ?>

1 个答案:

答案 0 :(得分:0)

纠正这个:

 if(!$error) {
     //search data if no errors
     $sql = "select * from subscribers";
     $conditions = array();

     if(!empty($firstname)){
     $conditions = "name='".$firstname."'";
     }

    if(!empty($lastname)){
    $conditions = "lastname='".$lastname."'";
    }

    if(!empty($sex)){
    $conditions = "gender='".$gender."'";
    }}