簡體   English   中英

多字段搜索(如果已填充)

[英]Multiple field search if filled

我想為多個字段進行搜索,如果任何字段被填充,則應該相應地執行搜索。 以下是我用於多個字段搜索的查詢

$select_us =("SELECT * FROM profiles WHERE (";
if($_GET['school']!='')
{
    $select_us.="SELECT * FROM profiles WHERE profile_id IN(select distinct school from education where school Like '$level%') ";
}
if($_GET['status']!='')
{
    $select_us.="SELECT * FROM profiles WHERE status Like '$level%'";
}
if($_GET['officee']!='')
{
    $select_us.="SELECT * FROM profiles WHERE Office Like '$Office%'";
}
select_us.=")");

我有一個如下的HTML表單

<form action="" method="get">

    <input name="keyword" style="width:70%;" class="wpcf7-form-control wpcf7-text" placeholder="Keyword" aria-invalid="false" type="text">

    <input name="last_name" style="width:70%;" class="wpcf7-form-control wpcf7-text" aria-invalid="false" placeholder="Last Name" type="text" onKeyUp="showResultf(this.value)"><div id="livesearchf" style=" transition: border linear .2s, box-shadow linear .2s; box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075); color:#000;  width:31%; font-size:16px; margin-top:-10px; float:left; background:#e9f2f7; overflow:hidden; max-height:400px; z-index:88; position:absolute; padding:0px 10px; line-height:22px; " ></div> 

    <input name="firs_name" style="width:70%;" placeholder="First Name"      class="wpcf7-form-control wpcf7-text" aria-invalid="false" type="text"  onKeyUp="showResultfirst(this.value)"><div id="livesearchfirst" style="color:#000;  width:31%; margin-top:-10px; float:left; background:#e9f2f7; overflow:hidden; max-height:400px; z-index:88; position:absolute; padding:0px   10px; line-height:22px; " ></div><br />

    <select style="width:70%;" name="status" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select a Level</option><?php $select_stauts =("SELECT distinct status FROM profiles where status!='' order by status asc ");
    $fetch_data_status = $conn->query($select_stauts);
    while($fetch_rows_status = $fetch_data_status->fetch_assoc())
    {
        $statuss=$fetch_rows_status['status']; ?><option value="<?php echo $statuss ; ?>"><?php echo $statuss ; ?></option><?php } ?></select><br />

    <select style="width:70%;" name="officee" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select an Office</option><?php $select_offcie =("SELECT distinct Office FROM profiles where Office!='' order by Office asc ");
    $fetch_data_office = $conn->query($select_offcie);  
    while($fetch_rows_office =           $fetch_data_office->fetch_assoc())
    {
        $office=$fetch_rows_office['Office']; ?><option value="<?php echo $office ; ?>">   <?php echo $office ; ?></option><?php } ?></select> 

    <select style="width:70%;" name="service_area" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select Service Area</option>  <?php $select_city =("SELECT  distinct city FROM profiles where city!='' order by city asc ");
    $fetch_data_city = $conn->query($select_city);
    while($fetch_rows_city =                           $fetch_data_city->fetch_assoc())
    {
        $city=$fetch_rows_city['city']; ?><option value="<?php echo $city ; ?>"><?php echo $city ; ?></option><?php } ?></select><br />

    <select name="baradmissionn" class="wpcf7-form-control wpcf7-select" aria- invalid="false" style="width:70%;"><option value="">Select Bar Admission</option>  <?php $select_bar =("SELECT distinct admitted FROM profiles order by admitted asc  ");
    $fetch_data_bar = $conn->query($select_bar);
    while($fetch_rows_bar = $fetch_data_bar->fetch_assoc())
    {
        $admitted=$fetch_rows_bar['admitted']; ?><option value="<?php echo $admitted   ;      ?>"><?php echo $admitted ; ?></option><?php } ?></select>

    <select name="school" style="width:70%;" class="wpcf7-form-control wpcf7-select" aria-invalid="false"><option value="">Select a School</option><?php $select_school =("SELECT distinct school FROM education where school!='' order by school asc ");
    $fetch_data_school = $conn->query($select_school);  
    while($fetch_rows_school = $fetch_data_school->fetch_assoc())
    {
        $schooll=$fetch_rows_school['school']; ?>
  <option value="<?php echo  $schooll ; ?>"><?php echo $schooll ; ?></option>   <?php } ?></select> <br />

    <input value="Search" class="wpcf7-form-control wpcf7-submit" name="search" type="submit" style="margin-left:60%;"></form>

請幫我查詢。 謝謝。

您不能以這種方式編寫查詢。

在下面進行查詢

    if($_GET['school']!='')
        {
           $condition .=' and profile_id in (select distinct school from education where school Like "$level%")'
        }
// make other condtion like this 
    $select_us ="SELECT * FROM profiles WHERE  1=1 ". $condition;

它會工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM