I am developing a Tour and Travel website and I have an option to allow my users to search Tours based on Region, Country and Duration.
Currently, the search option which I have developed allows to search tours and show results if all the search parameters match. For example, if a Tour is listed under Region- Africa, Country- Ethiopia and Duration- 5 Days, I have to select all the parameters to show up the Tour. But I want to modify the search option so that:
There would be a Select All option for Region, Country and Duration.
If someone searches using Select All option for all the three
parameters it would show all Tours available in the database.
If someone selects a Region only with the other two as Select All, it
would show only those tours which are listed under that Region. Same
with Country and Duration.
If someone selects only two parameters Like Country and Duration with
the third option as Select All, it would show only those tours which
are listed under the selected Country and Duration. Same with Region
and Duration.
Here is my Database Structure
The PHP code which I am using now is:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
$tour = mysql_query("select * from byp_tour where region='$region' and country='$country' and duration='$duration'");
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name";
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BYP Test</title>
</head>
<body>
<form action="byptest.php" method="post">
<div>
<label>Region</label>
<select id="region" name="region">
<option value="0">Select</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
</div>
<div>
<label>Country</label>
<select id="country" name="country">
<option value="0">Select</option>
<option value="1">Cambodia</option>
<option value="2">Thailand</option>
<option value="3">Vietnam</option>
<option value="4">Myanmar</option>
<option value="5">Laos</option>
<option value="6">Ethiopia</option>
<option value="7">France</option>
<option value="8">New York City</option>
<option value="9">Melbourne</option>
</select>
</div>
<div>
<label>Duration</label>
<select id="duration" name="duration">
<option value="0">Select</option>
<option value="1">5 Days</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
There are some possibilities to follow on your code.
You can let the 0 index option on the selects to be "All", instead of being "Select".
E.g.
The regions select
<select id="region" name="region">
<option value="0">Select</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
will be transformed into
<select id="region" name="region">
<option value="0">All</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
Another possibility is to create a new option with a specific index, e.g., 9999;
This way you can modify your query in order to select all the elements depending on your selection.
The code should be modified to:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
//define the index for the All option
$optionAllValue = 0; //add here the option index value used for the 'All' option
//define the where clause for the query
//in order to avoid many conditions verifications, we start it as 1=1
$whereClause = "1=1";
//now we check if the option selected for each field is not the value defined for the option 'All'
//this is just an example, and the best would be to create a function to avoid the replication of code
if($region != $optionAllValue)
{
$whereClause = $whereClause." and region='$region'";
}
if($country != $optionAllValue)
{
$whereClause = $whereClause." and country='$country'";
}
if($duration != $optionAllValue)
{
$whereClause = $whereClause." and duration='$duration'";
}
$query = "select * from byp_tour where ".$whereClause;
//original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
$tour = mysql_query($query);
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name";
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
The entire modified example can be found below:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
//define the index for the All option
$optionAllValue = 0; //add here the option index value used for the 'All' option
//define the where clause for the query
//in order to avoid many conditions verifications, we start it as 1=1
$whereClause = "1=1";
//now we check if the option selected for each field is not the value defined for the option 'All'
//this is just an example, and the best would be to create a function to avoid the replication of code
if($region != $optionAllValue)
{
$whereClause = $whereClause." and region='$region'";
}
if($country != $optionAllValue)
{
$whereClause = $whereClause." and country='$country'";
}
if($duration != $optionAllValue)
{
$whereClause = $whereClause." and duration='$duration'";
}
$query = "select * from byp_tour where ".$whereClause;
//original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
$tour = mysql_query($query);
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name";
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BYP Test</title>
</head>
<body>
<form action="byptest.php" method="post">
<div>
<label>Region</label>
<select id="region" name="region">
<option value="0">All</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
</div>
<div>
<label>Country</label>
<select id="country" name="country">
<option value="0">All</option>
<option value="1">Cambodia</option>
<option value="2">Thailand</option>
<option value="3">Vietnam</option>
<option value="4">Myanmar</option>
<option value="5">Laos</option>
<option value="6">Ethiopia</option>
<option value="7">France</option>
<option value="8">New York City</option>
<option value="9">Melbourne</option>
</select>
</div>
<div>
<label>Duration</label>
<select id="duration" name="duration">
<option value="0">All</option>
<option value="1">5 Days</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I have noticed your Selects are interdependents. It means you need to update the current options in cascade after on of the Selects are modified. To be able to do execute the cascading you can reference other questions on StackOverflow: cascading dropdowns from mysql with javascript and php
You have to build up your query depending on what variables are set. You could do something like:
$where = '';
if (!empty(($duration)) {
$where = " AND duration='$duration'";
}
if (!empty($country)) {
$where = " AND duration='$country'";
}
if (!empty($duration)) {
$where = " AND duration='$duration'";
}
$where = substr($where, 5);
$sql = "select * from byp_tour" . ($where ? " WHERE $where" : '');
$tour = mysql_query($sql);
Im no PHP whiz but I would first in your HTML form you would want a "Select All" (unless that's what you already mean by "Select")
If you did "Select All" Value = "0" Then after you check for form submission with your:
if(isset($_POST['submit'])){
YOu would then need if statements to check for any field that has a Value of "0". If a field is Set to 0 you would then want to remove that part of your database query.
For Ex. If Region = Select All then in your query remove region='$region'
That way you are not returning 0 results as your Region field should not have any region of 0.
Hope that kind of makes sense.
Related
Html Form:
<form>
<select name="country[]" id="country" multiple>
<option value="any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
</form>
PHP Code
<?php
$country = $_REQUEST['country'];
if($country=="")
$countrysql = "";
else
{
if($country == "Any") $countrysql = "";
else
{
$country = str_replace(",","','",$country);
$countrysql = " and Country in ('$country')";
}
}
$queryString = "SELECT * FROM register where $countrysql";
?>
I have created a form in PHP and I want to search multiple options. I already created table Register and a column Country. I am getting the result If I give single value. If I give multiple I am not getting the result. Please help.
You evaluate in if($country == "Any") the word Any is not equal to any in option <option value="any">any</option>
But I suggest this php code:
<?php
$country="";
$countryError="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["country"])){
$countryError = "Country is required";
}else{
$country = $_POST["country"];
}
if($country == "Any") {
$queryString = "SELECT * FROM register";
}else{
$queryString = "SELECT * FROM register where Country in ('$country')";
}
// Print the SQL string:
echo $queryString;
}
?>
The html tags:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="country" id="country" multiple>
<option value="Any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<span class="error"><?php echo $countryError;?></span>
I am currently doing a project in school which involves using a form to query a database. This form has multiple drop down menus and I am unsure on how to query the database if the user does not fill out all of the drop down menus. For example if the user only wants to search for a certain job type and does not specify the industry.
INDEX.HTML
<html>
<head>
</head>
<body>
<form action="test.php" method="post">
<select name="varjobtype">
<option value="nullg" disabled selected hidden>Job Type</option>
<option value="Part Time">Part Time</option>
<option value="Full Time">Full Time</option>
<option value="Contract">Contract</option>
<option value="Temporary">Temporary</option>
</select>
<select name="varindustry">
<option value="null" disabled selected hidden>Industry</option>
<option value="Accommodation and Food Services">Accommodation and Food Services</option>
<option value="Retail">Retail</option>
</select>
</form>
</body>
</html>
TEST.PHP
<html>
<head>
</head>
<body>
<?php
$jobtype = $_POST['varjobtype'];
$industry = $_POST['varindustry'];
$sql = "SELECT `Job ID`, Name, Employer FROM JobListings WHERE `Job Type` = '$jobtype' AND `Industry` = '$industry' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<div id="<? echo $row['Job ID']; ?>" class="box">
<?
echo "Job ID: " . $row["Job ID"]. "<br>";
echo "Name: " . $row["Name"]. "<br>";
echo "Employer: " . $row["Employer"]. "<br>";
echo "</div>";
}
?>
<?
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
As of now the php outputs no results.How would you make it so even if the user selects one of the drop down menus then the SQL statement will still display the jobs. Is it possible to make it so that it displays all of the jobs if the user does not interact with any of the drop down menus?
try this:
$jobtype = isset($_POST['varjobtype']) ? $_POST['varjobtype'] : '';
$industry = isset($_POST['varindustry']) ? $_POST['varindustry'] :'';
$sql = "SELECT `Job ID`, Name, Employer FROM JobListings";
$where = array();
if ($jobtype) $where[] = "`Job Type` = '".$jobtype."'";
if ($industry ) $where[] = "`Industry` = '".$industry."'";
if (!empty($where)) {
$sql .= " where " . implode (" and ",$where);
}
$result = $conn->query($sql);
...
I've been struggling for a while, trying to figure out how to connect the following query with the HTML search form.
Query:
<?php
include_once("mysqli_connection.php");
// startpage
$page = 0;
//items pr page
$per_page= 8;
//smileyscore
$smiley_score='';
// perform database query
$query = "SELECT * ";
$query .="FROM smile AS a, smiley_detail AS b ";
$query .= "WHERE a.smiley_id = b.smiley_id AND b.smiley >= 1 ";
$query .= "ORDER BY b.date ";
$query .= "LIMIT ". $page.', '. $per_page . ";";
$result = mysqli_query($connnetion, $query);
$row = mysqli_fetch_row($result);
$rows = $row[0];
if (!$result){
die("database query failed.");
}
?>
Form:
<div class="row">
<div class="col-xs-12">
<form class="menu" action="<?=$PHP_SELF?>" id="searchfor" method="post">
<select type="value" name="smiley_score" placeholder="s">
<option value="0">Alle stikprøver (0)</option>
<option value="1">Ingen anmærkninger (1)</option>
<option value="2">Indskærpelse (2)</option>
<option value="3">Påbud eller forbud (3)</option>
<option value="4">Bødeforlæg, politianmeldelse, autorisation eller registrering frataget (4)</option>
</select>
<select type="text" name="city">
<option value="">By</option>
<option value="København">København</option>
<option value="Århus">Århus</option>
<option value="Odense">Odense</option>
<option value="Alle de andre">Alle de andre</option>
</select>
<select type="value" name="year">
<option value="">År</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
<input type="text" name="searchcriteria" class="form-control" placeholder="Indtast søgekriterier"> </input>
<button class="btn" id="submit_button" type="submit" name="submit" value="Submit"> Søg</button>
</form>
</div>
</div>
Echo:
<div class="container">
<div class="row">
<?php
while($row = mysqli_fetch_assoc($result)){
// output data from each row
echo '<div class="col-md-12">';
echo '<div class="overskrift">';
echo '<h3>';
echo $row["organisation"] . "<br/>";
echo '</h3>';
echo 'Branche: '.$row["type"] . " - ";
echo 'Dato: '. $row["date"] . " <br /> ";
echo '</div >';
echo '<p class="adress">';
echo $row["adresse1"] . "<br /> ";
echo $row["zip"] . " ";
echo $row["postby"] . " ";
echo '<img class="score" src="img/'.$row["smiley"] .'.gif">';
echo '</p> ';
echo '<div class="textfield" >';
echo $row["text"] . "<br />";
echo '</div>';
echo '</div>';
}
// release data
mysqli_free_result($result);
?>
</div>
There are a few ways you could do this. On the page containing the form a pseudo structure like the following might be the answer.
<html>
<head><title>Searches and results....</title><head>
<body>
<?php
if( $_SERVER['REQUEST_METHOD']=='GET' ){
/* Display the form here */
} elseif( $_SERVER['REQUEST_METHOD']=='POST' ) {
/* Construct and run the sql query */
/* Display the results here */
} else {
/* Wrong method - warn user or redirect or whatever */
}
?>
</body>
</html>
You need process the data, you can get the data on the submited form by adding logic that uses the $_POST associative array to your $PHP_SELF, the name of the form element is what is used in array.
For example if you have an element named data, the PHP to access it would be $_POST['data'].
Make a condition in array form. Change the method as get method .
Customize your query like this
$sql = mysql_query("SELECT * FROM smile WHERE smiley_id LIKE '%$_GET[term]%' LIMIT $page,$_GET[results]");
I have a Tour Search application where user can search for available tours based on three different parameters- Region, Country and Duration. Currently the code which I am using is showing the Output Result in the same page. I want the output result to show in a different page.
Below is my PHP Code:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
//define the index for the All option
$optionAllValue = 0; //add here the option index value used for the 'All' option
//define the where clause for the query
//in order to avoid many conditions verifications, we start it as 1=1
$whereClause = "1=1";
//now we check if the option selected for each field is not the value defined for the option 'All'
//this is just an example, and the best would be to create a function to avoid the replication of code
if($region != $optionAllValue)
{
$whereClause = $whereClause." and region='$region'";
}
if($country != $optionAllValue)
{
$whereClause = $whereClause." and country='$country'";
}
if($duration != $optionAllValue)
{
$whereClause = $whereClause." and duration='$duration'";
}
$query = "select * from byp_tour where ".$whereClause;
//original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
$tour = mysql_query($query);
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name"; // HERE IS THE OUTPUT RESULT
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BYP Test</title>
</head>
<body>
<form action="searchtest.php" method="post">
<div>
<label>Region</label>
<select id="region" name="region">
<option value="0">All</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
</div>
<div>
<label>Country</label>
<select id="country" name="country">
<option value="0">All</option>
<option value="1">Cambodia</option>
<option value="2">Thailand</option>
<option value="3">Vietnam</option>
<option value="4">Myanmar</option>
<option value="5">Laos</option>
<option value="6">Ethiopia</option>
<option value="7">France</option>
<option value="8">New York City</option>
<option value="9">Melbourne</option>
</select>
</div>
<div>
<label>Duration</label>
<select id="duration" name="duration">
<option value="0">All</option>
<option value="1">5 Days</option>
<option value="2">10 Days</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
You need to add target="_blank" to <form>:
<form action="searchtest.php" method="post" target="_blank">
Actually i am performing a search in my database, there are two tables, rent and sale. When user selects parameter for rent with the help of radio button and other parameters, it should search the data in rent table and return the result if it all matches. and same for second table.
But after executing my query i am getting error. Please help me in this.
I am posting all my codes here.
User selection page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>real estate</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function()
{
$("input#tblsection").val('sale_table');
$('input:radio[name=rbtn]').attr('checked',true);
$("#rent").click(function() {
$("#rentselect").attr('style', 'display:block;margin-top:20px;');
$("#saleselect").attr('style', 'display:none;');
$("input#tblsection").val('rent_table');
});
$("#sale").click(function() {
$("#saleselect").attr('style', 'display:block;margin-top:20px;');
$("#rentselect").attr('style', 'display:none;');
$("input#tblsection").val('sale_table');
});
$("#srajax").click(function(){
var rentvalue = $('select#rentselect').val();
var salevalue = $('select#saleselect').val();
var location = $('select#location').val();
var area = $('select#Area').val();
var bedroom = $('select#bedroom').val();
var table = $('input#tblsection').val();
$.post(" testajax.php",
{
rentvalue: rentvalue,
salevalue: salevalue,
location: location,
area: area,
bedroom: bedroom,
table: table
},
function(data) {
alert("Data Loaded: "+data);
});
});
});
</script>
</head>
<body>
<form name="srajax" method="post">
<input type="radio" name="rbtn" value="rent" id="rent" /> <b>Rent</b>
<input type="radio" name="rbtn" value="sale" id="sale" /> <b>Sale</b>
<input type="hidden" name="tablenmae" id="tblsection" value="tablesale">
<br>
<select id="rentselect" style='display:none;'>
<option value="">----Select Budget For Rent----</option>
<option value="5000">5000</option>
<option value="5000_10000">5000 to 10000</option>
<option value="11000_20000">11000 to 20000</option>
<option value="above_20000">Above 20000</option>
</select>
<select id="saleselect" style='margin-top:20px;margin-bottom:20px;'>
<option value="">----Select Budget For Sale----</option>
<option value="5000">100000</option>
<option value="5000_10000">500000</option>
<option value="11000_20000">1000000</option>
<option value="above_20000">2500000</option>
</select>
<br>
<select id="location" name="Location">
<option>----Select Location---</option> <option value="All_Location">All Location</option>
<option value="Central_Mumbai">Central Mumbai</option>
<option value="Mumbai_Harbour">Mumbai Harbour</option>
<option value="Mumbai_Navi">Mumbai Navi</option>
<option value="Mumbai_South">Mumbai South</option>
<option value="Mumbai_Thane">Mumbai Thane</option> </select>
<br>
<br>
<select id="Area" name="Area">
<option value="">-----Select Area---</option>
<option value="All Area">All Area</option>
<option value="Bhandup">Bhandup</option>
<option value="Chembur">Chembur</option>
<option value="Kurla">Kurla</option>
<option value="Mulund">Mulund</option>
<option value="All Area">All Area</option>
<option value="Byculla">Byculla</option>
<option value="Chembur">Chembur</option>
<option value="Govandi">Govandi</option>
<option value="Sewri">Sewri</option>
<option value="Wadala">Wadala</option>
<option value="All Area">All Area</option>
<option value="Airoli">Airoli</option>
<option value="Belapur">Belapur</option>
<option value="Ghansoli">Ghansoli</option>
<option value="Mahape">Mahape</option>
<option value="Nerul">Nerula</option>
<option value="All Area">All Area</option>
<option value="Churchgate">Churchgate</option>
<option value="CST">CST</option>
<option value="Dadar">Dadar</option>
<option value="Fort">Fort</option>
<option value="All Area">All Area</option>
<option value="Brindavan">Brindavan</option>
<option value="Kalothe">Kalothe</option>
<option value="Kapur">Kapur</option>
<option value="Kalwa">Kalwa</option>
<option value="Kopat">Kopat</option> </select><br /><br>
<select id="bedroom" name="bedroom">
<option>---Select Bedroom---</option>
<option value="1BHK">1 BHK</option>
<option value="2BHK">2 BHK</option>
<option value="3BHK">3 BHK</option>
<option value="4BHK">3 BHK</option>
</select>
<br><br><br>
<input type="button" name="search" id="srajax" value="Search"/>
</form>
</body>
</html>
My search page
<?php
$host="localhost"; // Host name
$db_name="netelmbn_realestate"; // Database name
$tbl1_name="rent_table"; // Table1 name
$tbl2_name="sale_table"; //Table2 name
mysql_connect("localhost","netelmbn","password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
echo "<pre>";
print_r($_POST);
if($_POST['table'] == 'rent_table')
{
$result=mysql_query("select * from rent_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND $budget='budget' ");
echo "<table cellpadding='20'>";
echo '<tr>';
while ($row = mysql_fetch_assoc($result)) {
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
}
if($_POST['table'] == 'sale_table')
{
$result=mysql_query("select * from sale_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND $budget='budget' ");
echo "<table cellpadding='20'>";
echo '<tr>';
while ($row = mysql_fetch_assoc($result)) {
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
}
while ($row = mysql_fetch_assoc($result)) {
echo "<td width='300'><strong>Budget:</strong> ".$row['budget']."<br><strong>Location</strong>: ".$row['location']."<br><strong>Area:</strong> ".$row['area']."<br><strong>BHK:</strong> ".$row['bhk']."<br></td>";
}
echo "</tr></table>";
exit;
and i have rent_table
CREATE TABLE `rent_table` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`location` varchar(40) NOT NULL,
`area` varchar(40) NOT NULL,
`bedroom` varchar(20) NOT NULL,
`budget` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
samething for sale_table
please help me where i am going wrong. Here is the error image i am getting this error:
error http://netelity.com/realestate/error.png
After any and all database queries, check if the query was actually successful and use the appropriate debugging functions to find why it wasn't. Don't just assume that it worked.
$result = mysql_query(...);
if (!$result) {
echo mysql_error();
die;
}
ok here you go:
1) add this before if($_POST['table'] == 'rent_table') :
$location = $_POST['location'];
$area = $_POST['area'];
$bedroom = $_POST['bedroom'];
$budget = $_POST['budget'];
2) you have a mistake in your query : AND $budget='budget' should be AND budget='$budget'
That error indicates an error in the queries and therefore the MySQL resultset is invalid. The error seems to be in the check "$budget = 'budget'" that $budget should be enclosed in quotes. Try these versions:
Query1: $result=mysql_query("select * from rent_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND '$budget'='budget' ");
Query2: $result=mysql_query("select * from sale_table where location='$location' AND area='$area' AND bedroom='$bedroom' AND '$budget'='budget' ");
Hope it helps!