Forum Moderators: coopster
<form action="villa_search.php" method="post" name="form1">
Location:
<select name="location">
<option value='Florida Keys'>Florida Keys</option>
<option value='Atlantic Coast'>Atlantic Coast</option>
<option value='Gulf Coast'>Gulf Coast</option>
<option value='Orlando Disney Area'>Orlando Disney Area</option>
</select>
<br />
Property Type:
<select name="type">
<option value="*">Any</option>
<option value="Detached Villa">Detached Villa</option>
<option value="House">House</option>
<option value="Townhome">Townhome</option>
<option value="Terraced House">Terraced Home</option>
<option value="Condo">Condo</option>
<option value="Flat">Flat</option>
</select>
<br />
Bedrooms:
<select name="bedrooms">
<option value ="*">Any</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</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8+</option>
</select>
<br />
Villa ID:
<input name="id" type="text" />
<input name="Submit" type="submit" value="submit"/>
</form>
<?php
//Turn error reporting on, this will help loads when your developing!
error_reporting(E_ALL);
//Catch the form being processed
if(isset($_POST['Submit']) && ($_POST['Submit'] == "submit")){
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="test"; // Database name
$tbl_name="villas"; // Table name
// Connect to server and select database.
$conn = mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name, $conn)or die("cannot select DB");
//Assign the vars from the previous page
//still assign the security functions to the $_POST value
//Be aware though, that 'mysql_real_escape_string()' requires a DB connection to work
//but because there is already a connection going at this point in the script, the function
//will automatically pick it up without you needing to reference it in the function.
//if it doesn't pick it up just change the comments over
//they should both work though!
//$name = strip_tags(mysql_real_escape_string($_POST['name'], $conn));
$location = strip_tags(mysql_real_escape_string($_POST['location']));
$area = strip_tags(mysql_real_escape_string($_POST['area']));
$community = strip_tags(mysql_real_escape_string($_POST['community']));
$type = strip_tags(mysql_real_escape_string($_POST['type']));
$bedrooms = strip_tags(mysql_real_escape_string($_POST['bedrooms']));
$id = $_POST['id'];
//SQL query conducts a search according to the request
$sql = "SELECT `id`, `owner`, `location`, `area`, `community`, `type`, `bedrooms` FROM `".$tbl_name."` WHERE `location` = '".$location."' && `bedrooms` = '".$bedrooms."' || `id` = '".$id."'";
$query = mysql_query($sql, $conn) or die("cannot connect");
//see if the query returned anything ie > 'more than' 0 rows
if(mysql_num_rows($query) > 0){
//loop through the results
echo "<table border='1'>";
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array($query)) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['id']."<br>";
echo "<tr><td>";
echo $row['owner']."<br>";
echo "<tr><td>";
echo $row['location']."<br>";
echo "<tr><td>";
echo $row['area']."<br>";
echo "<tr><td>";
echo $row['community']."<br>";
echo "<tr><td>";
echo $row['type']."<br>";
echo "<tr><td>";
echo $row['bedrooms']."<br>";
}
//close while loop
}
else{
//set up the handler just in case the query returns no results!
echo "The query returned no results";
//optional, but if there is nothing else to do with the script you can kill it
exit;
}
//close the if
}
else{
//set the 'error handler' part of the script up
//if these files are in the same directory, and 'name' is the form this will redirect you back there
header("location: error.php");
}
//close the else
?>
... `bedrooms` = '*' ...
// By setting these null at the outset is simplifies your if's - see below.
$where=$location=$bedrooms=$type=null;
//
// Always check isset and empty.
if (isset($_POST['location']) and ! empty($_POST['location'])) {
$location = strip_tags(mysql_real_escape_string($_POST['location'], $conn));
}
//
if (isset($_POST['type']) and ! empty($_POST['type'])) {
$type = strip_tags(mysql_real_escape_string($_POST['type'], $conn));
}
if (isset($_POST['bedrooms']) and ! empty($_POST['bedrooms'])) {
$location = strip_tags(mysql_real_escape_string($_POST['bedrooms'], $conn));
}
if (isset($_POST['id']) and is_numeric($_POST['id'])) {
$id = $_POST['id'];
}
//
if ($location) {
// We need to parenthesize if there's an or
if ($id) { $where .= ' ('; } // note SPACE
$where .= " `location` = '$location'";
}
// We only need an AND if where has been started.
if ($type) {
if ($where) { $where .= ' and'; } // again, SPACE
else {
if ($id) { $where .= ' ('; }
}
$where .= " `type` = '$type'";
}
// Ditto
if ($bedrooms) {
// ( not needed **if** there's only one - this is the last one,
// but we'll add it anyway so it matches the ) below.
if ($where) { $where .= ' and'; } // again, SPACE
else {
if ($id) { $where .= ' ('; }
}
$where .= " `bedrooms` = '$bedrooms '";
}
// If $where and $id, obviously we started (
if ($where and $id) { $where .= ") or `id`='$id'"; }
else if ($id) { $where .= " `id`='$id'"; }
//
$sql = "SELECT `id`, `owner`, `location`, `area`, `community`, `type`, `bedrooms` FROM `$tbl_name`";
//
if ($where) { $sql .= " where $where"; } // again, SPACE
//
// Add an order and limit clause here.