AdRock Posted September 30, 2007 Share Posted September 30, 2007 I have a problem with some SQL queries so here is what i'me trying to do This is part of my form where I select a number of bedrooms <select name="minBeds" id="minBeds" style="width:152px"> <option value="Any">Any</option> <optgroup label="----"></optgroup> <option value="0">Studio</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5 or more</option> </select> and here is my query but it only works if I have selected a number and not 'Any' mysql_query("SELECT COUNT(*) FROM search WHERE (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type' AND `keywords` LIKE '%$keywords%') OR (price BETWEEN $min_price AND $max_price AND bedrooms >= $min_bedrooms AND location = '$location' AND type = '$property_type')") How do I do it so if a user selects any, it eliminates that from the query? Would I have to create some IF statements to check if it has been selected and create a new query? Link to comment https://forums.phpfreaks.com/topic/71252-how-to-eliminate-drop-down-selections-from-sql-query/ Share on other sites More sharing options...
pocobueno1388 Posted September 30, 2007 Share Posted September 30, 2007 Give This a try. <?php $query = "SELECT COUNT(*) FROM search WHERE (price BETWEEN $min_price AND $max_price": if (!empty($minBeds)) $query .= " AND bedrooms >= $min_bedrooms"; $query .= " AND location = '$location' AND type = '$property_type' AND `keywords` LIKE '%$keywords%') OR (price BETWEEN $min_price AND $max_price"; if (!empty($minBeds)){ $query .= " AND bedrooms >= $min_bedrooms"; $query .= " AND location = '$location' AND type = '$property_type')"; $result = mysql_query($query)or die(mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/71252-how-to-eliminate-drop-down-selections-from-sql-query/#findComment-358370 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.