php多个输入搜索表单在一个表databe上

时间:2016-06-09 07:38:52

标签: php mysql

我创建搜索表单并且它完全正常工作但我想在数据库的任何列中添加另一个字段来搜索字母。

这是我要添加的查询:

mysqli_query($conn,"SELECT * FROM tbl_subcontractor where db_RelatedCustome like '%$search%' or db_LevelofPriority like '%$search%' or db_Category like '%$search%' or db_Subcategories like '%$search%' or db_ProductOrService like '%$search%' or db_CompanyName like '%$search%' or db_Namecontactperson like '%$search%' or db_evaluation like '%$search%'  ")or die(mysqli_error($conn));

此图片解释表格:

这是我用于搜索的代码:

if(isset($_POST['submit'])) {
        $RelatedCustome=$_POST['txt_RelatedCustomer'];
        $LevelofPriority=$_POST['txt_LevelofPriority'];
        $category=$_POST['txt_category'];
        $subcategories=$_POST['txt_subcategories'];
        $ProductOrService=$_POST['txt_ProductOrService'];
        $CompanyName=$_POST['txt_CompanyName'];
        $Namecontactperson=$_POST['txt_Namecontactperson'];
        $evaluation=$_POST['txt_evaluation'];
        $search=$_POST['txt_search'];


    $q = array();  
        $sql = "";  
        if(isset($_POST['txt_RelatedCustomer']) && !empty($_POST['txt_RelatedCustomer'])){  
        $RelatedCustome = mysqli_real_escape_string($conn,$_POST['txt_RelatedCustomer']); 
        $q[] = " db_RelatedCustome='".$RelatedCustome."' ";      
        }  
        if(isset($_POST['txt_LevelofPriority']) && !empty($_POST['txt_LevelofPriority'])){  
        $LevelofPriority =  mysqli_real_escape_string($conn,$_POST['txt_LevelofPriority']); 
        $q[] = " db_LevelofPriority='".$LevelofPriority."' ";      
        }  
          if(isset($_POST['txt_category']) && !empty($_POST['txt_category'])){  
          $category =  mysqli_real_escape_string($conn,$_POST['txt_category']); 
        $q[] = " db_Category='".$category."' ";      
        } 
          if(isset($_POST['txt_subcategories']) && !empty($_POST['txt_subcategories'])){  
          $subcategories =  mysqli_real_escape_string($conn,$_POST['txt_subcategories']); 
        $q[] = " db_Subcategories='".$subcategories."' ";      
        } 
           if(isset($_POST['txt_ProductOrService']) && !empty($_POST['txt_ProductOrService'])){ 
        $ProductOrService =  mysqli_real_escape_string($conn,$_POST['txt_ProductOrService']);            
        $q[] = " db_ProductOrService='".$ProductOrService."' ";      
        } 
           if(isset($_POST['txt_CompanyName']) && !empty($_POST['txt_CompanyName'])){  
           $CompanyName =  mysqli_real_escape_string($conn,$_POST['txt_CompanyName']); 
        $q[] = " db_CompanyName='".$CompanyName."' ";      
        } 
           if(isset($_POST['txt_Namecontactperson']) && !empty($_POST['txt_Namecontactperson'])){  
           $Namecontactperson =  mysqli_real_escape_string($conn,$_POST['txt_Namecontactperson']); 
        $q[] = "db_Namecontactperson='".$Namecontactperson."' ";      
        } 
           if(isset($_POST['txt_evaluation']) && !empty($_POST['txt_evaluation'])){  
           $evaluation =  mysqli_real_escape_string($conn,$_POST['txt_evaluation']); 
        $q[] = "db_evaluation='".$evaluation."' ";      
        } 

        $first = true; 
        foreach($q as $qu){  
            if($first){  
            $sql .= " where ".$qu;      
            $first = false;  
            }else{  
            $sql .= " and ".$qu;          
            } 
        }  
        echo $sql;


        $result=mysqli_query($conn,"SELECT * FROM tbl_subcontractor {$sql}")or die(mysqli_error($conn));
        $count=mysqli_num_rows($result);
        if($count==0){echo"no result.";}
        else{
                echo"<table class='table table-hover table-responsive table-bordered' id='alternatecolor'>";
    echo"<tr>";
      echo" <td style='background:#f7ac01;font-size:16px;'>Related Customer</td>
            <td style='background:#f7ac01;font-size:16px;'>Level of Priority</td>
            <td style='background:#f7ac01;font-size:16px;'>Category</td>
            <td style='background:#f7ac01;font-size:16px;'>Subcategory</td>
            <td style='background:#f7ac01;font-size:16px;'>Product Or Service</td>
            <td style='background:#f7ac01;font-size:16px;'>Company Name</td>
            <td style='background:#f7ac01;font-size:16px;'>Name contact person</td>
            <td style='background:#f7ac01;font-size:16px;'>Evaluation</td>
            <td style='background:#f7ac01;font-size:16px;'>Phone</td>
            <td style='background:#f7ac01;font-size:16px;'>Phone 2</td>
            <td style='background:#f7ac01;font-size:16px;'>Whatsapp</td>
            <td style='background:#f7ac01;font-size:16px;'>Mobile</td>
            <td style='background:#f7ac01;font-size:16px;'>Web</td>
            <td style='background:#f7ac01;font-size:16px;'>Location</td>
            <td style='background:#f7ac01;font-size:16px;'>Comment</td>
            ";
     echo"</tr>";
        while($res=mysqli_fetch_array($result)){
        $RelatedCustome=$res['db_RelatedCustome'];
        $LevelofPriority=$res['db_LevelofPriority'];
        $category=$res['db_Category'];
        $subcategories=$res['db_Subcategories'];
        $ProductOrService=$res['db_ProductOrService'];
        $CompanyName=$res['db_CompanyName'];
        $Namecontactperson=$res['db_Namecontactperson'];
        $evaluation=$res['db_evaluation'];
        $phone=$res['db_Phone'];
        $phone_2=$res['db_phone2'];
        $whatsapp=$res['db_Phonewhatsapp'];
        $mobile=$res['db_mobile'];
        $web=$res['db_web'];
        $location=$res['db_Location'];
        $comment=$res['db_comment'];


        echo"<tr>";
    echo"<td >"; echo $RelatedCustome; echo"</td>";
    echo"<td >"; echo $LevelofPriority; echo"</td>";
    echo"<td>"; echo  $category; echo"</td>";
    echo"<td >"; echo $subcategories; echo"</td>";
    echo"<td >"; echo $ProductOrService; echo"</td>";
    echo"<td >"; echo $CompanyName; echo"</td>";
    echo"<td >"; echo $Namecontactperson; echo"</td>";
    echo"<td>"; echo $phone; echo"</td>";
    echo"<td>"; echo $phone_2; echo"</td>";
    echo"<td>"; echo $whatsapp; echo"</td>";
    echo"<td>"; echo $mobile; echo"</td>";
    echo"<td>"; echo $whatsapp; echo"</td>";
    echo"<td>"; echo $web; echo"</td>";
    echo"<td>"; echo $location; echo"</td>";
    echo"<td>"; echo $comment; echo"</td>";

    echo"</tr>";    

            }echo"</table>";
         }}

如何将查询与我的代码混合以获得完美的结果。

2 个答案:

答案 0 :(得分:0)

所有你的q数组中,你应该像这样使用'%'

  $q[] = "db_evaluation='%".$search."%'";   

并将此部分代码更改为

$first = true; 
foreach($q as $qu){  
    if($first){  
    $sql .= " where ".$qu;      
    $first = false;  
    }else{  
    $sql .= " or ".$qu;          
    } 
}  

答案 1 :(得分:0)

我找到了解决方案,感谢您抽出时间给我解决方案的想法

 $qq=array();
             if(isset($_POST['txt_search']) && !empty($_POST['txt_search'])){  
           $search =  mysqli_real_escape_string($conn,$_POST['txt_search']); 
        $qq[] = "db_evaluation like '%".$search."%' ";  
        $qq[] = "db_Namecontactperson like '%".$search."%' "; 
        $qq[] = " db_CompanyName like '%".$search."%' ";   
        $qq[] = " db_ProductOrService like '%".$search."%' ";  
        $qq[] = " db_Subcategories like '%".$search."%' "; 
        $qq[] = " db_Category like '%".$search."%' "; 
        $qq[] = " db_LevelofPriority like '%".$search."%' "; 
        $qq[] = " db_RelatedCustome like '%".$search."%' ";
        } 
     foreach($qq as $qu){  
            if($second){  
            $sql .= " where  ".$qu;      
            $second = false;  
            }else{  
            $sql .= " or ".$qu;          
            } 
        }