phpmysqlmultiple-selectmultiple-select-query

Multiple Select MySqL Query Using IN Operator?


I have been trying to make this webpage work for a while but keep getting problems. In this webpage, I have a series of selection boxes (some are independent and some are depended on another) to make selections and then apply filter to make a query. It is still in test-mode and working fine for single selections. But I have still not managed to make it work for multiple selections in same selection boxes. For example; When I select Europe and North America in Region Box and apply the filter it gives no result when I'd expect it to give me the results of the companies which are in Europe OR North America. You can find the test webpage here: http://gorevler.awardspace.biz/realdeal04.html

I have been trying to use "implode" and IN operator in .PHP file but don't know where I am doing wrong at. I'd appreciate it if you could you please show me the right way of doing it. You can find the coding below:

PHP

<?php

error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR |     MYSQLI_REPORT_STRICT);

$DB_HOST = "*****"; $DB_USER = "*****"; $DB_PASS = "*****"; $DB_NAME =   "*******";

$con = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if($con->connect_errno > 0) {       die('Connection failed [' . $con->connect_error . ']'); }


$bolge= "'" . implode("', '", $_POST['filtre_region']) . "'" ;
$bolge1= mysqli_real_escape_string($con,$bolge);

$ulke= "'" . implode("', '", $_POST['filtre_country']) . "'";
$ulke1= mysqli_real_escape_string($con,$ulke);

$sektor= "'" . implode("', '", $_POST['filtre_sector']) . "'";
$sektor1= mysqli_real_escape_string($con,$sektor);

$altsektor= "'" . implode("', '", $_POST['filtre_subsector']) . "'";
$altsektor1= mysqli_real_escape_string($con,$altsektor);;

$urun= "'" . implode("', '", $_POST['filtre_product']) . "'"; 
$urun1= mysqli_real_escape_string($con,$urun);

$sql = mysqli_query("SELECT * FROM gorevler WHERE region IN ('$bolge1') AND country IN ('$ulke1')     AND sector IN ('$sektor1') AND sub_sector IN ('$altsektor1') AND product IN ('$urun1')");

echo "<table border='0'>
<tr>
<th>No</th>
<th>Company</th>
<th>Region</th>
<th>Country</th>
<th>Sector</th>
<th>Sub Sector</th>
<th>Product</th>
<th>Website</th>
</tr>";

while ($row = mysqli_fetch_array($sql)){


echo "<td>" .  ''.$row['no'] . "</td>";
echo "<td>" . ''.$row['company'] . "</td>";
echo "<td>" . ''.$row['region'] . "</td>";
echo "<td>" . ''.$row['country'] . "</td>";
echo "<td>" . ''.$row['sector'] . "</td>";
echo "<td>" . ''.$row['sub_sector'] . "</td>";
echo "<td>" . ''.$row['product'] . "</td>";
echo "<td>" . ''.$row['website'] . "</td>";
echo "</tr>";
}
echo "</table>";

?>

HTML

<form id="filtersForm" action="search_company.php" method="post" target="_blank">

<fieldset id="filtersPane">

<div class="part03_line01" id="part03_line01">

  <div class="filter_bolge" id="filtre_bolge"><p>Region:</p>
   <select id="filter_region" name="filtre_region[]" class="select_bolge" title="Select a region" multiple="multiple" size="5">
     </select>    
      </div>

  <div class="filter_ulke" id="filtre_ulke"><p>Country:</p>
   <select id="filter_country" name="filtre_country[]" class="select_ulke" title="Select a   country" multiple="multiple" size="5">
     </select>    

  </div>   

  <div class="filter_sektor" id="filtre_sektor"><p>Sector:</p>
   <select id="filter_sector" name="filtre_sector[]" class="select_sektor" title="Select a  sector" multiple="multiple" size="5">
     </select>    

</div> 

<div class="filter_altsektor" id="filtre_altsektor"><p>Sub Sector:</p>
  <select id="filter_subsector" name="filtre_subsector[]" disabled="disabled"  class="select_altsektor" title="Select a sub-sector" multiple="multiple" size="5">
<option value="" data-filter-type="" selected="selected">
-- Make a Choice --</option>

</select>      
      </div>
<div class="filter_urun" id="filtre_urun"><p>Product:</p>
<select id="filter_product" name="filtre_product[]" disabled="disabled" class="select_urun"  title="Select a product" multiple="multiple" size="5">
<option value="" data-filter-type="" selected="selected">
-- Make a Choice --</option>

</select>   

</div>
</div>   

<div class="part03_line03" id="part03_line03">
  <div class="aramadugmesi" id="aramadugmesi"> <button type="submit" id="applyFilterButton">Apply Filters</button>
</div>         
</div>
</fieldset>  
</form> 

JAVASCRIPT

<script>

$(document).ready(function() { 

$('#filter_region')
.load('/textdata/region.txt');

$('#filter_country')
.load('/textdata/country.txt');


$('#filter_sector')
.load('/textdata/sector.txt');

$('#filter_sector').change(function() {
$('#filter_subsector').load("textdata/subsector/" + $(this).val() + ".txt",
function(){
$(this).attr('disabled',false);
}
);
});

$('#filter_subsector').change(function(){
$('#filter_product').load(
"textdata/subsector/product/" + $(this).val() + ".txt",
function(){
$(this).attr('disabled',false);
}
);
});
});
</script>

This Php coding is not working for me. It is not giving any results when I click Apply Filter. For example when I select Europe and North America in the selection box and click apply, I want all the companies which are in Europe OR North America to be fetched from database and listed. But it fetches no result. I guess it is a problem with php coding but I don't know whats wrong


Solution

  • You need to get single quotes inside your parentheses like this:

    $bolge1 = "'" . implode("', '", $_POST['filtre_region']) . "'";
    

    mysql needs to see something like this:

    IN ('value1','value2','value3')
    

    Your explode was just producing this :

    IN (value1, value2, value3)
    

    The code above will insert the opening and closing apostrophes and make sure there are also apostrophes between each value.