MySQL查询多个复选框值

时间:2015-02-15 02:56:53

标签: php mysql checkbox

我是复选框的新手。我想让用户根据复选框列表所代表的三个可能的过滤器进行搜索。例如,如果我使用下面的表格,我希望用户能够包括所有(红色或蓝色)和大的形状。我能够找到关于复选框查询的建议并没有完全解决这个问题。有没有办法用一个MySQL查询来做到这一点?

<form action="dbdquery.php" method="get">


<p>
Color:
<br/>
<input type="checkbox" name="color[]" value="Include All" checked/> Include All<br/>
<input type="checkbox" name="color[]" value="Red"/> Red<br/>
<input type="checkbox" name="color[]" value="Blue"/> Blue<br/>
<input type="checkbox" name="color[]" value="Yellow"/> Yellow<br/>

</p>

<p>
Size:
<br/>
<input type="checkbox" name="size[]" value="Include All" checked/> Include All<br/>
<input type="checkbox" name="size[]" value="Small"/> Small<br/>
<input type="checkbox" name="size[]" value="Medium"/> Medium<br/>
<input type="checkbox" name="size[]" value="Large"/> Large<br/>

</p>


<p>
Shape:
<br/>
<input type="checkbox" name="shape[]" value="Include All" checked/> Include All<br/>
<input type="checkbox" name="shape[]" value="Round"/> Round<br/>
<input type="checkbox" name="shape[]" value="Square"/> Square<br/>
<input type="checkbox" name="shape[]" value="Irregular"/> Irregular<br/>

</p>


    <input type="submit" value="Search">

</form>

1 个答案:

答案 0 :(得分:2)

试试这个:

使用内爆功能,

$ colors = implode(&#34;,&#34;,$ _GET [&#39; color&#39;]);

$ size = implode(&#34;,&#34;,$ _GET [&#39; size&#39;]);

$ shape = implode(&#34;,&#34;,$ _GET [&#39; shape&#39;]);

查询:

从表中选择*,其中($ colors)中的颜色或($ size)中的颜色或($ shape)中的形状;

您需要添加条件以选中包含所有内容。 (如果用户检查选择所有变量包括所有检查值)