我仔细考虑了最近的一篇文章并删除了它。希望这个版本能更清楚一些。
我有一个多选择下拉菜单,我希望能够过滤数据库表。
那么如果我有下面的下拉列表,大约有70个条目:
<select multiple="multiple" id="filter" name="filter">
<optgroup label="category1"> <!--jquery ui plugin allows for grouping -->
<option value="sub1">Subcategory 1</option>
<option value="sub2">Subcategory 2</option>
<option value="sub3">Subcategory 3</option>
</optgroup>
<optgroup label="category2">
<option value="sub4">Subcategory 4</option>
<option value="sub5">Subcategory 5</option>
<option value="sub6">Subcategory 6</option>
…
我有6个类别,我需要在我的sql语句中搜索,因此到目前为止,我有:
<?php
include ("connect.php");
$filter = $_POST["filter"];
$result = mysql_query("SELECT * FROM edt_images
WHERE category1= '$filter'
OR category2= '$filter'
OR category3 = '$filter'
OR category4 = '$filter'
OR category5 = '$filter'
OR category6 = '$filter'")
or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "<img src='files/small/thumb0_".$row['item_name'].".".$row['file_extension']."' border='0'/>";
}
?>
现在,如果我只从下拉菜单中选择一个条目,它就会工作,因为它会在六列中搜索该条目。我的问题是如何在这六列中搜索多个条目?
Thanks in advance
如果filter是一个选定类别的数组,您可以在查询中使用IN
:
$filterIn = implode("','",$filter);
SELECT * FROM edt_images
WHERE category1 IN ('$filterIn')
OR category2 IN ('$filterIn')
OR category3 IN ('$filterIn')
OR category4 IN ('$filterIn')
OR category5 IN ('$filterIn')
OR category6 IN ('$filterIn')
请注意,您不应该在SQL查询中使用未经处理的用户输入。
首先你需要允许从你的多重选择返回多个值。为此,您需要向name属性添加括号,如:
<select multiple="multiple" id="filter" name="filter[]">