Hello I need to filter data from database. Use selects the data he need from selectboxes and inputs. Not all inputs must be filled. Those that arent have value of -1. I have put together a SQL statement that should work. Probably would work without the prepare but I need to have it there. I need to know what is wrong with this code. Why is the WHERE caluse not working there. PS. If i set $where = "1=1" then I can get all data but when I use the code I have which generates WHERE = autod.mark=7 then I dont get any data.
$response['filter'] = $filter->make;
$whereClauses = array();
if ( $filter->make > 0 ) $whereClauses[] = 'autod.mark='.mysql_real_escape_string($filter->make);
if ( $filter->color > 0 ) $whereClauses[] ='autod.varv='.mysql_real_escape_string($filter->color);
if ( $filter->model > 0 ) $whereClauses[] = 'autod.mudel='.mysql_real_escape_string($filter->model);
if ( !empty($filter->yearFrom) && is_numeric($filter->yearFrom) ) $whereClauses[] = 'aasta >= '.mysql_real_escape_string($filter->yearFrom);
if ( !empty($filter->yearTo) && is_numeric($filter->yearFrom) ) $whereClauses[] = 'aasta =< '.mysql_real_escape_string($filter->yearTo);
if ( !empty($filter->priceFrom) && is_numeric($filter->priceFrom) ) $whereClauses[] = 'hind >='.mysql_real_escape_string($filter->priceFrom);
if ( !empty($filter->priceTo) && is_numeric($filter->priceFrom) ) $whereClauses[] = 'hind =<'.mysql_real_escape_string($filter->priceTo);
$where = '';
if (count($whereClauses) > 0) {
$where .= implode(' AND ',$whereClauses);
}
$response['filter'] = $where;
$cmd = $connect->prepare("SELECT autod.id, mark.mark, mudel.mudel, varv.varv, aasta, hind, kirjeldus FROM autod
INNER JOIN mark ON autod.mark = mark.id
INNER JOIN varv ON autod.varv = varv.id
INNER JOIN mudel ON autod.mudel = mudel.id
WHERE ?
");
$cmd->bind_param("s", $where);