Jump to content

Handling Empty queries


adam.jones2

Recommended Posts

Check the input, and only add the WHERE when input is present, e.g.

$query = "SELECT * FROM site_cars";$filters = array();if (!empty($Make)) { $filters[] = "car_Make = $Make";}if (!empty($Min_Price) && !empty($Max_Price)) { $filters[] = "car_price BETWEEN $Min_Price AND $Max_Price";}if (!empty($filters)) {$query .= ' WHERE ' . implode(' AND ', $filters);}

Link to comment
Share on other sites

Iv gone with this approch. any ideas why it dont work?

$Make = $_POST['make'];$Min_Price = $_POST['pf'];$Max_Price = $_POST['pt'];//check if there was tried a searchif ($_POST["make"] ||$_POST["pf"] || $_POST["pt"]) {//ok, this was a search$query =  " car_make= " . $_POST["make"] . "";if ($Min_Price || $Max_Price)$query= " (car_price >= " . (int) $_POST["pf"] . "" . ($_POST["pt"] ? " AND car_price <= "  . (int) $_POST["pt"] . "" : "")  . ") ";// Search Array    if (is_array($query)) {    $cars = mysql_query("SELECT * FROM site_cars WHERE" . implode(" AND " , $query) . " ORDER BY car_price desc");    } else die('<p class="error">######.</p>');    $numresults=mysql_query ($query);    if(!$numresults) {    echo mysql_error();	 exit;}

Link to comment
Share on other sites

You're missing a bracket here:

if ($Min_Price || $Max_Price)$query

should be

if ($Min_Price || $Max_Price) {$query

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...