我想根据以下表单过滤mysql结果
www.dabndash.com/filter.php
我一直在尝试的过滤器的mysql代码是:
SELECT DISTINCT * FROM paintings
WHERE (price BETWEEN '$min_price' AND '$max_price')
AND (orientation = '$orientation1' OR orientation = '$orientation2' OR orientation = '$orientation3')
AND (size_diagonal BETWEEN '$size_min' AND '$size_max')
AND (primary_color = '$primary_color' AND secondary_color = '$secondary_color')
我不确定为什么查询不会过滤结果。即使用户输入了任何一个字段,它也应该过滤结果。
答案 0 :(得分:0)
您应该只使用用户选择的参数并从中构建查询。用户似乎可以将方向,大小和原色滤镜留空。
例如,如果$orientation1
为空,则查询将返回零结果(前提是所有绘画都具有非空方向。
尝试使用较少的where子句运行查询,并验证是否获得了预期的结果。
答案 1 :(得分:-1)
SELECT DISTINCT * FROM paintings WHERE (price BETWEEN '$min_price' AND '$max_price')
AND (orientation = '$orientation1' OR orientation = '$orientation2' OR orientation = '$orientation3')
AND (size_diagonal BETWEEN '$size_min' AND '$size_max')
AND (primary_color = '$primary_color' AND secondary);