Jump to content

my search script returns results even when an empty search term


dazzclub

Recommended Posts

Hi guys,

 

my search script returns results even when i enter no term to search and just press enter.

 

I have made some changes to the script and also removed some code that i thought may have caused this but when i test it out nothing changed.

 

If anyone can help me on this that will be great! :)

 

Here is the script i am using

---------------

<?php require_once("includes/connection.php"); ?>
<?php
if ( (isset($_GET['ID'])) && (is_numeric($_GET['ID'])) ) { //correctly accessed 
$id=$_GET['ID']; 
} else { 
$errors[] = 'You have accessed this page incorrectly.'; 
} 


{
	$sql = "SELECT * FROM products WHERE product_id = $id";

	If ($r = mysql_query ($sql)) { 
	//sending the query to the mySQL server
	While ($row = mysql_fetch_array($r)) { 
	//inputs the data into the table


	$product_id = $row['product_id'];
	$name = $row['name'];
	$type = $row['type'];
	$image = $row['image'];
	$close_up = $row['close_up'];
	$category = $row['category'];
	}
}
}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
	<title>Game cards & Labels</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="javascript/lightbox.js"></script>		
</head>
<body>
<div id="container">
	<div id="holder">
	<div id="header"><?php include("includes/top-inlcude.php"); ?></div>
	<div id="topNav"><?php include("includes/main-nav.php"); ?></div>
	<div id="content">
	<div id="left"><?php include("includes/left-hand-coloumn.php"); ?></div>
	<div id="middle">
		<h2>Search results</h2>
		Your results for search term "<span style="font-weight:bold;color:red;"><?php echo $_POST['search']; ?></span>"
		<div style="width:100%;height:5px;border-bottom:1px solid black;"></div>
		<?php 
require_once("includes/connection.php"); 


$rowsPerPage = 5;

$pageNum = 1;

If(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}	
$offset = ($pageNum - 1) * $rowsPerPage;

$search = mysql_real_escape_string($_POST['search']);



$query = "SELECT * FROM  products WHERE name LIKE \"%$search%\" OR category LIKE  \"%$search%\" OR image LIKE  \"%$search%\" OR type LIKE \"%$search%\" ORDER BY product_id  DESC" . " LIMIT $offset, $rowsPerPage" ;
$result = mysql_query ($query)or die(mysql_error() . "<p>With query:<br>$query");

while ($row = mysql_fetch_assoc($result)){
echo "<ul style=\"font-weight:bold;list-style-type:none;padding:4px;margin:0px;\">";
echo "<li>";
echo $row['name'];
echo "</li>";
echo "<li>";
echo $row['type'];
echo "</li>";
echo "<li>";
echo "<img src=\"images/product-images/$row[image]\" style=\"border:1px solid black;\" />";
echo "</li>";
echo "<li>";
echo $row['category']. '<br />';
echo "</li>";
echo "<li><a href=\"display-product.php?ID=$row[product_id]\" style=\"color:red;\" title=\"$row[name]\"/>more information >></a></li>";
echo "</ul>";
echo "<div style=\"width:100%;height:5px;border-bottom:1px solid black;\"></div>";
   
}
$query = "SELECT COUNT(*) As numrows FROM products";
$result = mysql_query($query) or die ('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];


$maxPage = ceil($numrows/$rowsPerPage);


$self = $_SERVER['PHP_SELF'];
$nav  = '';

for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
	$nav .=" $page "; 
}
else
{$nav .=" <a href=\"$self?page=$page\" style=\"padding:0px;margin:0px;\">$page</a> ";
}
}

if ($pageNum > 1 )
{
	$page = $pageNum - 1;
	$prev = "<a href=\"$self?page=$page\" style=\"padding:0px;margin:0px;\">[Prev]</a> ";

	$first = " <a href=\"$self?page=1\" style=\"padding:0px;margin:0px;\">[first page]</a>";
}
else
{
   $prev  = ' '; // we're on page one, don't print previous link
   $first = ' '; // nor the first page link
}

if ($pageNum < $maxPage)
{
   $page = $pageNum + 1;
   $next = " <a href=\"$self?page=$page\" style=\"padding:0px;margin:0px;color:black;\">[Next]</a> ";

   $last = " <a href=\"$self?page=$maxPage\" style=\"padding:0px;margin:0px;\">[Last Page]</a> ";
} 
else
{
   $next = ' '; // we're on the last page, don't print next link
   $last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
// and close the database connection
?>

	</div>
	<div id="right"><?php include("includes/right-hand-coloumn.php"); ?></div>
	<div id="footer"><?php include("includes/footer.php"); ?></div>
</body>
</html>	

--------------

 

Regards

Dazzclub

 

Please use code tags ~ CV

could have been an error in your if statements (maybe). Give this a try:

<?php require_once("includes/connection.php"); ?>
<?php
if (isset($_GET['ID']) && is_numeric($_GET['ID'])) { //correctly accessed 
$id=$_GET['ID']; 
} else { 
$errors[] = 'You have accessed this page incorrectly.'; 
} 


{





$sql = "SELECT * FROM products WHERE product_id = $id";










If ($r = mysql_query ($sql)) { 





//sending the query to the mySQL server





While ($row = mysql_fetch_array($r)) { 





//inputs the data into the table















$product_id = $row['product_id'];





$name = $row['name'];





$type = $row['type'];





$image = $row['image'];





$close_up = $row['close_up'];





$category = $row['category'];





}



}
}




?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>



<head>





<title>Game cards & Labels</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="javascript/lightbox.js"></script>








</head>



<body>



<div id="container">





<div id="holder">





<div id="header"><?php include("includes/top-inlcude.php"); ?></div>





<div id="topNav"><?php include("includes/main-nav.php"); ?></div>





<div id="content">





<div id="left"><?php include("includes/left-hand-coloumn.php"); ?></div>





<div id="middle">







<h2>Search results</h2>







Your results for search term "<span style="font-weight:bold;color:red;"><?php echo $_POST['search']; ?></span>"







<div style="width:100%;height:5px;border-bottom:1px solid black;"></div>







<?php 
require_once("includes/connection.php"); 


$rowsPerPage = 5;

$pageNum = 1;

If(isset($_GET['page']) && $_GET['page'] != "")
{



$pageNum = $_GET['page'];
}




$offset = ($pageNum - 1) * $rowsPerPage;

$search = mysql_real_escape_string($_POST['search']);



$query = "SELECT * FROM  products WHERE name LIKE \"%$search%\" OR category LIKE  \"%$search%\" OR image LIKE  \"%$search%\" OR type LIKE \"%$search%\" ORDER BY product_id  DESC" . " LIMIT $offset, $rowsPerPage" ;
$result = mysql_query ($query)or die(mysql_error() . "<p>With query:<br>$query");

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



echo "<ul style=\"font-weight:bold;list-style-type:none;padding:4px;margin:0px;\">";



echo "<li>";



echo $row['name'];



echo "</li>";



echo "<li>";



echo $row['type'];



echo "</li>";



echo "<li>";



echo "<img src=\"images/product-images/$row[image]\" style=\"border:1px solid black;\" />";



echo "</li>";



echo "<li>";



echo $row['category']. '<br />';



echo "</li>";



echo "<li><a href=\"display-product.php?ID=$row[product_id]\" style=\"color:red;\" title=\"$row[name]\"/>more information >></a></li>";



echo "</ul>";



echo "<div style=\"width:100%;height:5px;border-bottom:1px solid black;\"></div>";
   
}
$query = "SELECT COUNT(*) As numrows FROM products";
$result = mysql_query($query) or die ('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];


$maxPage = ceil($numrows/$rowsPerPage);


$self = $_SERVER['PHP_SELF'];
$nav  = '';

for($page = 1; $page <= $maxPage; $page++)
{



if ($page == $pageNum)



{





$nav .=" $page "; 



}



else



{$nav .=" <a href=\"$self?page=$page\" style=\"padding:0px;margin:0px;\">$page</a> ";
}
}

if ($pageNum > 1 )
{





$page = $pageNum - 1;





$prev = "<a href=\"$self?page=$page\" style=\"padding:0px;margin:0px;\">[Prev]</a> ";










$first = " <a href=\"$self?page=1\" style=\"padding:0px;margin:0px;\">[first page]</a>";
}
else
{
   $prev  = ' '; // we're on page one, don't print previous link
   $first = ' '; // nor the first page link
}

if ($pageNum < $maxPage)
{
   $page = $pageNum + 1;
   $next = " <a href=\"$self?page=$page\" style=\"padding:0px;margin:0px;color:black;\">[Next]</a> ";

   $last = " <a href=\"$self?page=$maxPage\" style=\"padding:0px;margin:0px;\">[Last Page]</a> ";
} 
else
{
   $next = ' '; // we're on the last page, don't print next link
   $last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
// and close the database connection
?>













</div>





<div id="right"><?php include("includes/right-hand-coloumn.php"); ?></div>





<div id="footer"><?php include("includes/footer.php"); ?></div>



</body>
</html>

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.