I have a form that submits the following:
State City Category
My query needs to do the following:
If only state selected - show state results
If state and cat selected - show state and cat results
if state and city selected - show state and city results
if state, city and cat are selected - show state, city, cat results
The catch is when they submit the city, 3 different fields need to be checked for a match
This is my query and it doesnt work, I have tried 100 different variations. Sorry for being such a newbie. Thanks for help in advance.
$sql = "SELECT * FROM items WHERE state=$st AND city1='$_GET[city] OR
city2='$_GET[city]' OR city3='$_GET[city]' OR cat='$_GET[cat]'
AND active='1'
ORDER BY item_id DESC LIMIT $limitvalue, $limit ";
$result=mysql_query($sql);
while ($prow=mysql_fetch_array($result))
{
I personally am not very good at doing MySQL query strings, I generally use an sql compiler class similar to this (keep in mind, this is just a quick one). It allows for binding, which you require, especially with
$_GET
values like you have:Gives you:
Note, this requires you to use a proper connection (PDO works best in this case).
your code contains typo , use
Check on 1st line of your code you have
city1='$_GET[city] OR .......
which is wrongcity1='$_GET[city]' OR
is correctLooks like operator precedence? When you are grouping related conditions where only one of which must be true, you want something of this form:
You may want to read up on SQL injection, too.