2017-01-12 2 views
0

Я хочу сделать serach для для muliple полей, например, если какое-либо поле заполнено, то поиск должен выполняться соответствующим образом. ниже запрос, который я использую для нескольких полей поискаНесколько полей поиска, если они заполнены

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

Пожалуйста, помогите мне с запросом. Благодарю.

+0

см http://stackoverflow.com/a/8149183/5701450 – Akshay

ответ

1

Вы не можете писать запрос таким образом.

делают запрос ниже, как

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; 

он будет работать