Jump to content

Ajax search showing results whith no input


mikejs

Recommended Posts

Hi I have created a search page using ajax and mysql to display the search results the search works fine, however if I press search without any input it displays the whole database all results ?? driving me crazy any help much appretiated

 

this is my search.php :-

 

if($type == "results")
{
  //run query where search value is like table fields
  $sql = "SELECT * FROM articles WHERE ( ";
  $sql .= " UPPER(title) LIKE '%" . $query . "%' OR ";
  $sql .= " UPPER(category) LIKE '%" . $query . "%' OR ";
  $sql .= " UPPER(body) LIKE '%" . $query . "%' ";
  $sql .= ") order by category";
  
  $q = (mysql_query($sql));
// echo $q;
  $num_rows = mysql_num_rows($q); ///check no of rows
  
if($num_rows>0){
 echo "    <b>Your search for the word<font color=blue> $query </font>returned $num_rows results: -\n</b>";
 echo "<div id=\"scrollWrapper\">";
 echo "<div id=\"scroll\">";
// while there are rows echo output to produce page of results
  
while($d = mysql_fetch_array($q)){
	 $id = $d['id'];
	 $title = $d['title'];
  	 $category = $d['category'];
	 $body = $d['body'];
 $ref = $d['ref'];

/// produces page of results  

  echo "<div class =\"results\"><p><strong>Category: </strong>" . $category . "<br>";// using a div to be able to manipulate the output in css 
  echo '<strong>Title: </strong>' . $title . '<br>';
  echo '<br>';
  echo '' . $body . '<br>';
  echo '<br>';
  echo '<b>Reference: ' . $ref . '</b><hr>';
  echo "</div>";


//end while loop
  }else
{
	 echo"<p>Sorry, no search result. Please change keywords and try again.</p>";
}//end if 

echo "</div>";
echo "</div>";
}
mysql_close($con);
?>

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.