Forum Moderators: coopster

Message Too Old, No Replies

Form Input and SELECT command problem

SELECT command syntax

         

AliTaylor4411

9:52 am on Aug 7, 2010 (gmt 0)

10+ Year Member



Basically I have five drop down lists where the user will choose

Minimum Price
Maximum Price
Bedrooms
Property Type
Area

When I make selections from the boxes and press search I just get a list of all the entries in the database and not as per the selections made.

Here is my form

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index Page</title>
<link href="homesltdstylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form method="post" action="1indexpageb.php">
<table width="80%" border="0" cellpadding="1" class="LOGO">
<tr class="logo_table">
<td width="14%" align="left" valign="middle"><img src="../images/home-logo.png" alt="LOGO" width="137" height="127" align="absmiddle" /> HOMES LIMITED ESTATE AGENTS</td>
</tr>
</table>
<table width="80%" border="0" cellpadding="0" class="navigationbar">
<tr class="navigation_bar">
<td width="16%">SALE</td>
<td width="16%">LET</td>
<td width="17%">VALUATION</td>
<td width="17%">CONVEYANCING</td>
<td width="16%">MORTGAGES</td>
<td width="16%"><p>CONTACT US</p></td>
</tr>
</table>
<table width="80%" border="0" cellpadding="0">
<tr>
<td colspan="5" align="left" valign="top" class="property_of_week"><p>&nbsp;</p></td>
</tr>
<tr>
<td colspan="2" align="left" valign="middle" class="property_of_week"><p>Property of the Week - 40 Pink Road, Whelley Wigan</p></td>
<td height="37" colspan="3" align="left" valign="middle" class="quicksearch_header" ><p>SEARCH PROPERTY DATABASE</p></td>
</tr>
<tr>
<td width="12%" rowspan="7" align="left" valign="top" class="key_features"><p class="key_features">&nbsp;</p>
<p class="key_features">Key Features </p>
<ul>
<li class="key_features">Detached</li>
<li class="key_features">Large Plot</li>
<li class="key_features">Double Glazed</li>
<li class="key_features">Gas Central Heating</li>
<li class="key_features">Mature Gardens</li>
<li class="key_features">Stamp Duty Paid</li>
</ul></td>
<td width="56%" rowspan="7" align="left" valign="top" class="picturebox"><img src="../images/detached1.jpg" alt="P4" width="510" height="338" align="right" class="key_features"></td>
<td height="5%" colspan="2" align="left" valign="top" class="quick_search">Minimum Price</span></td>
<td width="14%" align="left" valign="top" class="quick_search" >
<span class="quick_search">
<select name = "minprice">
<option value="no minimum">No Minimum</option>
<option value="£60,000">£60,000</option>
<option value="£100,000">£100,000</option>
<option value="£125,000">£125,000</option>
<option value="150,000">£150,000</option>
<option value="£175,000">£175,000</option>
<option value="£200,000">£200,000</option>
<option value="£225,000">£225,000</option>
<option value="£250,000">£250,000</option>
<option value="£275,000">£275,000</option>
<option value="£300,000+">£300,000+</option>
</select>
</span></td>
</tr>
<tr>
<td height="5%" colspan="2" align="left" valign="top" class="quick_search">Maximum Price</span></td>
<td align="left" valign="top" class="quick_search" >
<span class="quick_search">
<select name = "maxprice">
<option value="no maximum">No Maximum</option>
<option value="£60,000">£60,000</option>
<option value="£100,000">£100,000</option>
<option value="£125,000">£125,000</option>
<option value="150,000">£150,000</option>
<option value="£175,000">£175,000</option>
<option value="£200,000">£200,000</option>
<option value="£225,000">£225,000</option>
<option value="£250,000">£250,000</option>
<option value="£275,000">£275,000</option>
<option value="£300,000+">£300,000+</option>
</select>
</span></td>
</tr>
<tr>
<td height="5%" colspan="2" align="left" valign="top" class="quick_search">Bedrooms</span></td>
<td align="left" valign="top" class="quick_search" >
<span class="quick_search">
<select name = "bedrooms" class="quicksearch_header">
<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>
</select>
</span></td>
</tr>
<tr>
<td height="5%" colspan="2" align="left" valign="top" class="quick_search">Property Type</span></td>
<td align="left" valign="top" class="quick_search" ><span class="quick_search">
<select name = "property_type">
<option value="Apartment">Apartment</option>
<option value="Terraced">Terraced</option>
<option value="Semi-Detached">Semi-Detached</option>
<option value="Detached">Detached</option>
<option value="Bungalow">Bungalow</option>
</select>
</span></td>
</tr>
<tr>
<td height="5%" colspan="2" align="left" valign="top" class="quick_search">Area</span></td>
<td align="left" valign="top" class="quick_search" >
<span class="quick_search">
<select name = "area">
<option value="Newsprings">Newsprings</option>
<option value="Pemberton">Pemberton</option>
<option value="Whelley">Whelley</option>
<option value="Whitley">Whitley</option>
</select>
</span></td>
</tr>
<tr>
<td colspan="3" valign="top" class="block_colour_yellow"><input type="submit" value="SEARCH" />&nbsp;</td>
</tr>
<tr>
<td colspan="3" valign="top" class="block_colour_yellow">&nbsp;</td>
</tr>
<tr>
<td colspan="2" rowspan="17" align="left" valign="top"><span class="description_text">Are you looking for a family home with MORE than the X FACTOR? This could be the home for you... A stunning family home located in one of Wigan’s most prestigious locations within easy reach of Wigan town centre, Mesnes Park, Haigh Hall and all local amenities. Fully refurbished to high standards throughout with luxurious finishes in every room making this a truly elegant and stylish property. Its many features include: stunning Howarth kitchen with granite work surfaces, master suite with en-suite shower room and en-suite dressing room and high gloss porcelain marble flooring to the ground floor. Viewings are now invited to fully appreciate and we strongly recommended early viewings to avoid any disappointment.
</p>
</span>
<p class="description_text">Detached house comprising briefly of: hall, ground floor wc, boiler room, lounge, dining room, stunning kitchen, split level landing, master suite with en-suite shower room and en-suite dressing room, three additional bedrooms and principal bathroom. </p>
<p class="description_text">Access is via electric gates giving access to the spacious driveway and integral garage, providing off road parking space for multiple vehicles.</p>
<p></p></td>
<td colspan="3" valign="top" class="officeuse_header">OFFICE USE</td>
</tr>
<tr valign="top">
<td colspan="2" class="office_use">LOGIN</td>
<td class="office_use">&nbsp;</td>
</tr>
<tr valign="top">
<td colspan="2" class="office_use">PASSWORD</td>
<td class="office_use">&nbsp;</td>
</tr>
<tr valign="top">
<td height="25%" colspan="3" class="block_colour">&nbsp;</td>
</tr>
<tr valign="top">
<td colspan="3" class="office_text">OFFICES</td>
</tr>
<tr valign="top">
<td width="15%" class="office_text">1 King Street</td>
<td colspan="2" class="office_text">2 Queen Street</td>
</tr>
<tr valign="top">
<td class="office_text">Whitley</td>
<td colspan="2" class="office_text">Pemberton</td>
</tr>
<tr valign="top">
<td class="office_text">Wigan</td>
<td colspan="2" class="office_text">Wigan</td>
</tr>
<tr valign="top">
<td class="office_text">WN11NW</td>
<td colspan="2" class="office_text">WN22NW</td>
</tr>
<tr valign="top">
<td class="office_text">Tel: 01942666666</td>
<td colspan="2" class="office_text">Tel:01942555555</td>
</tr>
<tr valign="top">
<td colspan="3" class="block_colour_yellow">&nbsp;</td>
</tr>
<tr valign="top">
<td class="office_text">3 Jack Road</td>
<td colspan="2" class="office_text">4 Ace Road</td>
</tr>
<tr valign="top">
<td class="office_text">Whelley</td>
<td colspan="2" class="office_text">Standish</td>
</tr>
<tr valign="top">
<td class="office_text">Wigan</td>
<td colspan="2" class="office_text">Wigan</td>
</tr>
<tr valign="top">
<td class="office_text">WN33NW</td>
<td colspan="2" class="office_text">WN44NW</td>
</tr>
<tr valign="top">
<td class="office_text">Tel: 01942888888</td>
<td colspan="2" class="office_text">Tel: 01942999999</td>
</tr>
<tr valign="top">
<td colspan="3" class="block_colour">&nbsp;</td>
</tr>
</table>
<table width="80%" border="0" cellpadding="1" class="navigationbar">
<tr class="navigation_bar">
<td width="16%">SALE</td>
<td width="16%">LET</td>
<td width="17%">VALUATION</td>
<td width="17%">CONVEYANCING</td>
<td width="16%">MORTGAGES</td>
<td width="16%"><p>CONTACT US</p></td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>


And here is my processessing script:

<?php
// This page retrieves data from the database table "property" and this code should be used to create the list of properties.

//Open the connection
$conn= mysql_connect("exampledb.com","exampledb", "examplepw");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}

//Select the Database
mysql_select_db("ahtaylor1db", $conn);


//Create the MySQL Command to retrieve the record
$sql = "SELECT * FROM property";


//Execute the MySQL statement and convert the result to an array
$result = mysql_query($sql, $conn);
while ($array = mysql_fetch_array($result)){
echo "Here are the results of your search"."<br>";
echo "<p>Property Address:" . $array[propertyadd1]. $array[propertyadd2]. $array[propertyadd3]. $array[postcode]. "<br>";
echo "Property Type" . $array[propertytype]."<br>";
echo "Bedrooms: " . $array[bedrooms]."<br>";
echo "Price: " . $array[price]."<br>";
echo "Description: " . $array[description]."</p>";
}
?>

I presume the $sql ="Select * from property"; is not correct and should be something like

$sql= "SELECT * from property
where minprice = $_POST[minprice]
AND maxprice = $_POST[maxprice]
AND bedrooms = $_POST[bedrooms]
AND propertytype =$_POST[propertytype]
AND area = $_POST[area];

would this pull the selections from the form and just show the results which match the criteria or can anyone help me with the SELECT command so it will do this!

Thanks



but could anyone give me an idea on how I can get my search to work and only pull out the entries which correspond with the search selections?

HELP!

Matthew1980

10:06 am on Aug 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there AliTaylor4411,

Bad syntax - it should be this:-

echo "<p>Property Address:".$array['propertyadd1'].$array['propertyadd2'].$array['propertyadd3'].$array['postcode']. "<br>\n\r";

If you had error reporting turned on you would get an undefined index - presumed constant error ;)

And, your right about the syntax for the sql query, try this instead:-

$sql= "SELECT * FROM `property`
WHERE `minprice` = '".$_POST['minprice']."'
AND `maxprice` = '".$_POST['maxprice']."'
AND `bedrooms` = '".$_POST['bedrooms']."'
AND `propertytype` = '".$_POST['propertytype']."'
AND `area` = '".$_POST['area']."' ";


Once you have this in you will need to have some form of error handling in place in case the parameters you have stipulated return 0 rows. And as this is a form, you need really to have a 'catch me i have been submitted' handler there too, this will deal with erroneous submissions.

And please use some form of data sanitising before you use $_POST directly into a sql statement, as this is the best way to protect your information - with out it, someone can put DROP `yourDb`; in the input and WIPE your information. I cannot stress this point enough.

And for future reference, please try not to post HUGE code dumps as this deters lots of people from responding, other than that, all is well ;)

Hope that's clearer now.

Cheers,
MRb

AliTaylor4411

3:47 pm on Aug 7, 2010 (gmt 0)

10+ Year Member



Hi Matthew1980

Thanks for the information. Will only show the code with problems in future.

AliTaylor4411

AliTaylor4411

8:50 pm on Aug 7, 2010 (gmt 0)

10+ Year Member



Hi Matthew1980

Used the code as suggested but now getting another error message saying

Parse error: syntax error, unexpected T_WHILE in /home/ahtaylor1/ahtaylor1.bimserver2.com/1indexpageb.php on line 27

This is the code I have used:

//Create the MySQL Command to retrieve the record
$sql= "SELECT * FROM `property`
WHERE 'minprice' = '".$_POST['minprice']."'
AND 'maxprice' = '".$_POST['maxprice']."'
AND 'bedrooms' = '".$_POST['bedrooms']."'
AND 'propertytype' = '".$_POST['propertytype']."'
AND 'area' = '".$_POST['area']."' ";


//Execute the MySQL statement and convert the result to an array
$result = mysql_query($sql, $conn)
while $array = (mysql_fetch_array($result)){

echo "Here are the results of your search"."<br>";
echo "<p>Property Address:".$array['propertyadd1'].$array['propertyadd2'].$array['propertyadd3'].$array['postcode']. "<br>\n\r";
echo "Property Property Type" . $array['propertytype']."<br>";
echo "Property Bedrooms: " . $array['bedrooms']."<br>";
echo "Property Price: " . $array['price']."<br>";
echo "Description: " . $array['description']."</p>";
}
?>

Any suggestions?

AliTaylor4411

9:10 pm on Aug 7, 2010 (gmt 0)

10+ Year Member



I changed

while $array = (mysql_fetch_array($result)){

to

while ($array = mysql_fetch_array($result)){

which has stopped the error message but which displays a blank page?

Any suggestions

rocknbil

1:36 am on Aug 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reply in your other thread does this a bit more gracefully, but the problem is you have a mysql syntax error - you do not quote field names.

AND 'bedrooms' = '".$_POST['bedrooms']."'

should be

AND bedrooms = '".$_POST['bedrooms']."'

or

AND `bedrooms` = '".$_POST['bedrooms']."'


You may backtick them but this is only needed if they confilct with internal commands or reserved words.

Here's how you avoid "mystery malfunctions without apparent cause" in the future. Also, you will rarely get an exact match on someone searching for minimum and maximum, and in the context of properties, there's usually one price. And what if someone only submits some of these fields? You won't get a match on any records if they are all hard coded in like this, agan, see your other thread.

In the context of this particular post, note the bolded additions and removed quotes:

//Create the MySQL Command to retrieve the record
$sql= "SELECT * FROM `property`
WHERE price >= '".$_POST['minprice']."'
AND price <= '".$_POST['maxprice']."'
AND bedrooms >= '".$_POST['bedrooms']."'
AND propertytype = '".$_POST['propertytype']."'
AND area = '".$_POST['area']."' ";

echo "$sql<br>";

//Execute the MySQL statement and convert the result to an array
$result = mysql_query($sql, $conn) or die("could not execute the query: " . mysql_error());

If it does not error and display a mysql error, copy and paste the results of the echoed select into the mySQL command line (or phpMyadmin). It would mean you have no matching records.

Matthew1980

6:13 pm on Aug 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Gah! My phone doesn't let me see all of the thread, I have had to wait until back home :(

AND 'bedrooms' = '".$_POST['bedrooms']."'

should be

AND bedrooms = '".$_POST['bedrooms']."'

or

AND `bedrooms` = '".$_POST['bedrooms']."'


As Rocknbil correctly states, you don't necessarily need to 'back tick' field names, admittedly this is just a preference of mine to do this, I find that doing it is a good habit to get into if there is a space in the field name or a reserved word in mysql, without back ticks this would error (I think), so purely as a preference of mine, I always back tick.

I blame dodgy copy & pasting ;p

Cheers,
MRb

AliTaylor4411

9:50 pm on Aug 10, 2010 (gmt 0)

10+ Year Member



Still cannot get a result from my form. Here is the select command I am using followed by the conversion to array code. Has anyone got any idea what I have done wrong?

//Create the MySQL Command to retrieve the record
$sql= "SELECT * FROM `property`
WHERE `minprice` >= '".$_POST['minprice']."'
AND `maxprice` <= '".$_POST['maxprice']."'
AND `bedrooms` >= '".$_POST['bedrooms']."'
AND `propertytype` = '".$_POST['propertytype']."'
AND `propertyadd` = '".$_POST['area']."' ";

//Execute the MySQL statement and convert the result to an array
$result = mysql_query($sql, $conn);
while ($array = mysql_fetch_array($result)) {
echo "<p>Property Address:". $array['propertyadd1'].$array['propertyadd2'].$array['propertyadd3'].$array['postcode']. "<br>\n\r";
echo "Property Type:" . $array['propertytype']."<br>\n\r";
echo "Bedrooms:" . $array['bedrooms']."<br>\n\r";
echo "Price:" . $array['price']."<br>\n\r";
echo "Description:" . $array['description']."</p>";

Matthew1980

10:07 pm on Aug 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there AliTaylor4411,

Does the data populate the sql query as Rocknbil asked in a previous post?

If so does that query work in your sql query browser or php myadmin when you paste it in there?

If that does as it should, what does this do? :-
$sql= "SELECT * FROM `property`
WHERE `minprice` >= '".$_POST['minprice']."'
AND `maxprice` <= '".$_POST['maxprice']."'
AND `bedrooms` >= '".$_POST['bedrooms']."'
AND `propertytype` = '".$_POST['propertytype']."'
AND `propertyadd` = '".$_POST['area']."' ";

//Execute the MySQL statement and convert the result to an array
$result = mysql_query($sql, $conn);
while ($array = mysql_fetch_array($result)){
echo "<pre>";
print_r($array);
echo "</pre>";
}

If this produces anything your going to need to check the names as you are referencing when building the html.

All of this is based on the assumption that all of the fields on the form ARE being transmitted from that to this script...

Hope this makes sense,

Cheers,
MRb

LifeinAsia

10:28 pm on Aug 10, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Did you echo $sql? Does it look like you would expect (with the correct values passed from the form)?

AliTaylor4411

10:41 pm on Aug 10, 2010 (gmt 0)

10+ Year Member



I have noticed a few spelling mistakes in the code which I have now rectified and which has stopped the error message.

I have added:

echo "$sql<br>"; underneath the SELECT command which on doing a search (query) prints out :

SELECT * FROM `property` WHERE `price` >= '60000' AND `price` <= '275000' AND `bedrooms` >= '4' AND `propertytype` = 'Detached' AND `propertyadd3` = 'Whelley'

So I copied this into myphpadmin and it returned the following message:

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0110 sec)

Does this mean that the search isnt finding a property which matches the criteria chosen during the search? It should find a property within the parameters as there is a property in the database?!

Any ideas?

Matthew1980

8:53 am on Aug 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there AliTaylor4411,
>>MySQL returned an empty result set (i.e. zero rows). (Query took 0.0110 sec)

This means as there were no results returned as the search criteria didn't match anything in the DB, you now need to debug that to see what needs tweaking to get it to return the results you are expecting.

Personally at this stage, I would recommend you writing down (working out) what you will expect from you search pattern, because php/mysql will only return the results you ask of it.

At least you know now that the sql statement is being populated correctly :) Half the job is now complete, you just need to work the kinks out from the mysql side..

Cheers,
MRb

AliTaylor4411

9:52 am on Aug 11, 2010 (gmt 0)

10+ Year Member



Thanks Matthew1980

Getting there at long last!

AliTaylor4411

12:16 pm on Aug 11, 2010 (gmt 0)

10+ Year Member



Hi Guys

Just letting you know I have sorted the problem and now get a result! You were right Matthew1980. I found an error in my database (wrong spelling!)

Now getting the returns I expect.

Thanks again - What a Relief!

Matthew1980

12:39 pm on Aug 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there AliTaylor4411,

>>I found an error in my database (wrong spelling!) Now getting the returns I expect.

Great, hopefully now you have learned a few tricks on the way, and that should stand you in good stead for the future.

Happy Coding.

Cheers,
MRb