使用IN运算符多选择MySqL查询?

问题描述:

我一直在努力使这个网页工作一段时间,但不断得到问题。在这个网页中,我有一系列的选择框(一些是独立的,一些是依赖另一个)进行选择,然后应用过滤器进行查询。它仍然处于测试模式,并且对单个选择工作正常。但是我仍然没有设法在相同的选择框中为多个选择工作。例如;当我在Region Box中选择欧洲和北美并应用过滤器时,如果我期望它能给我在欧洲或北美的公司的业绩,则不会给出结果。你可以在这里找到测试网页:http://gorevler.awardspace.biz/realdeal04.html使用IN运算符多选择MySqL查询?

我一直试图在.PHP文件中使用“implode”和IN运算符,但不知道我在哪里做错了。如果你能请你告诉我正确的做法,我会很感激。你可以找到下面的编码:

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> 

ŧ他的PHP编码不适合我。点击应用过滤器时,不会显示任何结果。例如,当我在选择框中选择欧洲和北美,然后单击“应用”时,我希望从数据库中提取所有在欧洲或北美的公司并将其列出。但它没有取得任何结果。我想这是与PHP的编码问题,但我不知道什么是错

+0

而你的问题是? – hakre

+0

这个PHP编码不适合我。点击应用过滤器时,不会显示任何结果。例如,当我在选择框中选择欧洲和北美,然后单击“应用”时,我希望从数据库中提取所有在欧洲或北美的公司并将其列出。但它没有取得任何结果。我想这是一个与PHP编码的问题,但我不知道什么是错的? – barutto

+0

你抑制警告吗?你需要在'IN'的每一种可能性附加单引号('''),当它们是字符串时 – kero

你需要让你的括号内单引号是这样的:

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

MySQL需要看到这样的事情:

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

你的爆炸只是生产这样的:

IN (value1, value2, value3) 

上面的代码将插入运算引用和关闭撇号并确保每个值之间也有撇号。

+0

所以基本上我在PHP文件中使用“implode”和“IN”结构是正确的方法来从多个选择数据库中获取记录? – barutto

+0

在PHP中有不同的格式化字符串的方法,这种方法对于你需要的结果很好。 –

+1

如何正确逃脱? – Gumbo