Jump to content

PHP form retrieving results from MySQL 'first search' problem.


RyanW67

Recommended Posts

Hey Guys,

 

I'm new to this forum and haven't really been studying PHP relatively long, having a bit of an odd one at the moment though...

Basically I followed a tutorial, and have now customised the search for my own purpose, the problem I'm having is that the first time I search via my form... it prints alls the rows from my table, however - after then whenever I put in my own criteria it works fine...?

 

Below is the code im using for my search.php which my html form points at... (i've taken out the security details for obv reasons) hopefully I've complied to all the posting rules... thanks!

 

<?php

$hostname =  "";
$username =  "";
$password =  "";
$dbName =  "";
$table = "houses";

MYSQL_CONNECT($hostname, $username, $password) OR DIE( "Unable to connect
to database");
$number = 0;
$house_title = @$_GET['house_title'] ;
$house_desc = @$_GET['house_desc'] ;
$house_rooms = @$_GET['house_rooms'] ;
$house_area = @$_GET['house_area'] ;
$house_type = @$_GET['house_type'] ;
$house_pic = @$_GET['house_pic'] ;
$house_price = @$_GET['house_price'] ;

@mysql_select_db("$dbName") or die(  "Unable to select database"); 

                   
                   if ($house_area == "house_area")
                      {$house_area = '%';}

                      if ($house_rooms == "house_rooms")
                      {$house_rooms = '%';}

                      if ($house_price == "house_price")
                      {$house_price = '%';}
                   
                      if ($house_desc == "")
                       {$house_desc == '%';}
                      
                      if ($house_pic == "")
                       {$house_pic == '%';}

                      if ($house_title == "")
                        {$house_title == '%';}

                   

$query =  ("SELECT * FROM $table WHERE house_area LIKE '$house_area%' AND 
house_rooms LIKE '$house_rooms%' AND house_price LIKE '$house_price%'");


$result = MYSQL_QUERY($query);



  /* Determine the number of records returned */ 

while ($row=MYSQL_FETCH_ROW($result))
$number = mysql_numrows($result);


if  ($number == 0) 
{ 
print ("Unfortunately, there are no properties which match your criteria.");
}
if ($number != 0)
{

	  /* Print the relevant information */ 
	$i = 0;
			 echo  "<font face=\"Verdana, Arial, Helvetica, sans-serif\">
			 <b>There are $number records in the inventory:</b></font><p>";
			 echo  "<table cellpadding=5>";
			 echo  "    <TR bgcolor=black> <td><font face=\"Verdana, 
	Arial,Helvetica, sans-serif\" size=\"-1\" color=white><b>House Title</b></font></td> <td><font face=\"Verdana, Arial, Helvetica,
			  sans-serif\" size=\"-1\" color=white><b>Rooms</b></font></td> <td><font face=\"Verdana, Arial, Helvetica, 
			  sans-serif\" size=\"-1\" color=white><b>Price</b></font></td>
			  <td><font face=\"Verdana, Arial, Helvetica,
			 sans-serif\" size=\"-1\" color=white><b>Area</b></font></td>
			 <td><font face=\"Verdana, Arial, Helvetica,
			 sans-serif\" size=\"-1\" color=white><b>Description</b></font></td>
			 <td><font face=\"Verdana, Arial, Helvetica,
			 sans-serif\" size=\"-1\" color=white><b>Picture</b></font></td>
			</tr>";

	while ($i < $number):
		$house_title = mysql_result($result, $i,"house_title");
		$house_rooms = mysql_result($result, $i,"house_rooms");
		$house_price = mysql_result($result,$i,"house_price");
		$house_area = mysql_result($result,$i,"house_area");
		$house_desc = mysql_result($result,$i,"house_desc");  
		$house_pic = mysql_result($result,$i,"house_pic");    

			if ($i%2 == 0) {
			echo  "<tr bgcolor=lightgrey>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\">$house_title</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_rooms</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_price</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_area</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_desc</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_pic</font></td>
	</tr>";
			} else {
			echo  "<tr bgcolor=lightgreen>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\">$house_title</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_rooms</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_price</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_area</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_desc</font></td>
	 <td><font face=\"Verdana, Arial, Helvetica,
	sans-serif\" size=\"-1\"> $house_pic</font></td>
	</tr>";
		}
	$i++;
	endwhile;
	echo  "</table>";
}
/* Close database connection */ 
MYSQL_CLOSE();
?>

When you say you enter your own criteria and it works fine, do you mean you just change the source code and change the query getting rid of all the variables?

 

How are you variables passed onto the form? Is the method of the form "post" or "get"?

When you say you enter your own criteria and it works fine, do you mean you just change the source code and change the query getting rid of all the variables?

 

How are you variables passed onto the form? Is the method of the form "post" or "get"?

 

Sorry, I mean entering own criteria via my form, there 3 drop down menus in my form for area, rooms and maxprice. And im using get. thanks.

 

edit: appologies don't think Im being clear - basically when my page loads up with the form in, if I just leave the the form as it is without selecting anything (there are default values) and press search, then it loads up the results with all the rows printed out, though if I change the criteria, it searches and returns the correct results  everytime..... not sure why this is happening.

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.