2014-05-25 41 views
-2

我一直在努力使這個網頁工作一段時間,但不斷得到問題。在這個網頁中,我有一系列的選擇框(一些是獨立的,一些是依賴另一個)進行選擇,然後應用過濾器進行查詢。它仍然處於測試模式,並且對單個選擇工作正常。但是我仍然沒有設法在相同的選擇框中爲多個選擇工作。例如;當我在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

回答

1

你需要讓你的括號內單引號是這樣的:

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

MySQL需要看到這樣的事情:

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

你的爆炸只是生產這樣的:

IN (value1, value2, value3) 

上面的代碼將插入運算引用和關閉撇號並確保每個值之間也有撇號。

+0

所以基本上我在PHP文件中使用「implode」和「IN」結構是正確的方法來從多個選擇數據庫中獲取記錄? – barutto

+0

在PHP中有不同的格式化字符串的方法,這種方法對於你需要的結果很好。 –

+1

如何正確逃脫? – Gumbo

相關問題