Jump to content

Search function


spearchilduser

Recommended Posts

hi im just trying to do a search on a database and display results based on what the user selected:

 

form code:

html>
<body>
<form action="search.php" method="POST">

Type of property:
   <select name="Type_of_Property">
                            <option>Terraced</option>
						<option>Detached</option>
						<option>Semi-Detached</option>
						<option>Bungalow</option>
						<option>Flat </option>                     


						</select>
<p><input type="submit" value="Send Details" name="B1"></p>

</form>

 

php code:

<html>

<body>
<?php
error_reporting(E_ALL);


mysql_connect("localhost", "root") or die(mysql_error());											// makes a connection
mysql_select_db("estate_agents") or die('I cannot connect to the database because: ' . mysql_error()); //conects to the database

$result = mysql_query("SELECT * FROM properties WHERE Type_of_Property ='".$_POST['Type_of_Property']);


echo "<table border=1>\n";
echo "<tr><td>Address 1</td><td>Address 2</td><td>Postcode</td><td>Type of Property</td><td>Number of Bedrooms</td><td>Number of Bathrooms</td></tr>\n";

while ($row = mysql_fetch_row($result)) {


}
print  "Data base updated with: " .$_POST["Type_of_Property"] ;
?>

</body>
</html>

 

the propbelm im having is being able to display the results in the table in the while loop

 

any help is appreciated thank you

Link to comment
https://forums.phpfreaks.com/topic/254712-search-function/
Share on other sites

use mysql_fetch_array() or mysql_fetch_assoc() not mysql_fetch_row().  If you use mysql_fetch_assoc() then you will need to use the actual column names within the square brackets and single quotes rather then the numerical array key value.

while ($row = mysql_fetch_array($result)) {
echo"<tr><td>{$row['0']}</td><td>{$row['1']}</td>...<td>{$row['n']}</td></tr>";
}// remember to close your table after the results are written out

Link to comment
https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306049
Share on other sites

The output is the users entry on the entry form i was just usign it to make sure the correct data was beign passed through the Post function;

 

However the error that has come up when i put the error checking in is

error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Terraced' at line 1

 

 

Link to comment
https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306065
Share on other sites

1. missing closing quote

2. put queries in string so you can echo for debugging

 

 

try changing this...

 

$result = mysql_query("SELECT * FROM properties WHERE Type_of_Property ='".$_POST['Type_of_Property']);

 

to this...

$search_for = .$_POST['Type_of_Property']);
$query = "SELECT * FROM properties WHERE Type_of_Property ='search_for'";
$result = mysql_query($query);

Link to comment
https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306081
Share on other sites

1. missing closing quote

2. put queries in string so you can echo for debugging

 

 

try changing this...

 

$result = mysql_query("SELECT * FROM properties WHERE Type_of_Property ='".$_POST['Type_of_Property']);

 

to this...

$search_for = .$_POST['Type_of_Property']);
$query = "SELECT * FROM properties WHERE Type_of_Property ='search_for'";
$result = mysql_query($query);

You missed the $ off the start of search_for in the $query string.

Link to comment
https://forums.phpfreaks.com/topic/254712-search-function/#findComment-1306135
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.