php - Query mysql which a simple to show particular records -
i have simple db of real estate property listings. trying simple search show records. eg property location, type , price eventually. having trouble select statement if users submit blank fields. see code. there better way this? ok on bit displays records using while loop. newbie sure can tell.
<form action="search.php" method="post"> <table border="0" cellpadding="10" cellspacing="0"> <tr> <td width="300"><b>reference no</b></td> <td><input type="text" name="reference_no" maxlength="20" id="reference_no" /></td> </tr> <tr> <td><b>property name</b></td> <td><input type="text" name="property_name" maxlength="30" id="property_name" /></td> </tr> <tr> <td><b>property area</b></td> <td> <select name="property_area" id="property_area"> <option value="ortaca">ortaca</option> <option value="sarigerme">sarigerme</option> <option value="other">other</option> </select> </td> </tr> <tr> <td><b>property type</b></td> <td><select name="property_type" id="property_type"> <option value="apartment">apartment</option> <option value="land">land</option> <option value="commercial property">commercial property</option> </select></td> </tr> <tr> <td></td> <td> <input class="formsubmit" type="submit" value="submit enquiry" name="submit" /> </td> </tr> </table> </form> $reference_no = $_post['reference_no']; $property_name = $_post['property_name']; $property_area = $_post['property_area']; $property_type = $_post['property_type']; $query = "select * properties (live = 'yes' , property_type = '$property_type') , (reference_no = '$reference_no' or property_name = '$property_name' or property_area = '$property_area') order property_area, property_price";
if understand correctly want properties if nothing chosen, , limit selection user choices .
on sql server use intersect in following way:
$filters[] = "select * properties" if ($property_type) $filters[] = "select * properties property_type = '$property_type'"; if ($reference_no) $filters[] = "select * properties reference_no = '$reference_no'"; if ($property_name) $filters[] = "select * properties property_name = '$property_name'"; if ($property_area) $filters[] = "select * properties property_area = '$property_area'"; $final_query = implode(' intersect ', $filters)
but mysql not support intersect. in case wrap filter on filter on filter ... etc
$sql = 'select * properties'; if ($property_type) $sql = "select * ({$sql}) q property_type = '$property_type'"; if ($reference_no) $sql = "select * ({$sql}) q reference_no = '$reference_no'"; if ($property_name) $sql = "select * ({$sql}) q property_name = '$property_name'"; if ($property_area) $sql = "select * ({$sql}) q property_area = '$property_area'";
and yes, don't forget either escape/sanitize values prior inserting them query string or use parameterized query pdo or mysqli style.
Comments
Post a Comment