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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.