adam.jones2 Posted February 16, 2012 Share Posted February 16, 2012 I have a PHP/SQL Query $query = "SELECT * FROM site_cars WHERE car_Make = $Make AND car_price BETWEEN $Min_Price AND $Max_Price"; If the $Min_Price AND $Max_Price is equal to "" the query fails to run. is there a way around this? Link to comment Share on other sites More sharing options...
Ingolme Posted February 16, 2012 Share Posted February 16, 2012 Don't run the query if the values are empty. You're supposed to test that all the values are what they should be before the query. Link to comment Share on other sites More sharing options...
adam.jones2 Posted February 16, 2012 Author Share Posted February 16, 2012 im using a search form where if nothing is selected the value is "" is there a way around this? Link to comment Share on other sites More sharing options...
boen_robot Posted February 16, 2012 Share Posted February 16, 2012 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 More sharing options...
adam.jones2 Posted February 16, 2012 Author Share Posted February 16, 2012 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 More sharing options...
adam.jones2 Posted February 21, 2012 Author Share Posted February 21, 2012 get error Parse error: syntax error, unexpected '}' in C:\wamp\www\SEARCH FORM\quicksearch.php on line 31 Link to comment Share on other sites More sharing options...
justsomeguy Posted February 21, 2012 Share Posted February 21, 2012 It sounds like you have a bracket that is unmatched, or maybe a missing semicolon on the previous line. Link to comment Share on other sites More sharing options...
boen_robot Posted February 23, 2012 Share Posted February 23, 2012 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.