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);
?>

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.