Reputation: 1649
I'm trying to create searchfunction in PHP/MySQL. It works for some products but not for others. I can't figure out why that is the case.
I'm using the fields 'Product naam' and 'Merk' from this table: http://piclair.com/data/4192d.jpg
The searchfunction DOES work for searchquery's like 'cohiba' or 'punch'. But it doesn't work for searchquery's like 'sigarillos' or 'aansteker'. Also, when no product is found, it doesn't show an error message and I don't know why.
Here is the relevant part of the code:
<?php
$invoer = $_POST['zoekwoord'];
?>
<a href="index.php">Home</a> / <a href="zoekresultaten.php" class ="selected"><?php echo $invoer; ?></a>
<?php
//verbind met database
include("login/con1.inc");
$page=$_GET["page"]; //Get the page number to show
If($page == "")
{
$page=1; //If no page number is set, the default page to 1
}
//eerst wordt de query opgehaald om te bepalen hoeveel pagina's nodig zullen zijn.
$sql = "SELECT '".$invoer."'
FROM product
ORDER BY productnaam"; // dit is dus de query om de producten op te halen
$query = mysql_query($sql) or die (mysql_error()."<br>in file ".__FILE__." on line ".__LINE__); // als de query niet uitgevoerd wordt geeft die een foutmelding met bestandsnaam en regelnummer
$num = mysql_num_rows($query); // Het aantal producten
$Limit = 6; //Number of results per page
$NumberOfPages = ceil($Num/$Limit);
$sql = mysql_query("SELECT * FROM product WHERE merk='".$invoer."' AND Categorie_Nummer = 1 ORDER BY productnaam LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());
if(empty($num)) {
echo "<p>Er zijn geen producten gevonden.</p>\n";
} else {
// Laat de producten zien
while($product = mysql_fetch_object($sql)) {
?>
<table border="5" class="productlist" width="400px">
blablablabla
Hmm. While trying to create an error message I broke the searchfunction itself. Again it doesn't work for 'aansteker' etc. Also the error message doesn't work yet.
Here's the code I have now:
<div id="contentbox">
<?php
$invoer = mysql_real_escape_string($invoer);
$invoer = $_POST['zoekwoord'];
?>
<a href="index.php">Home</a> / <a href="zoekresultaten.php" class ="selected"><?php echo $invoer; ?></a>
<?php
//verbind met database
include("login/con1.inc");
$page=$_GET["page"]; //Get the page number to show
If($page == "")
{
$page=1; //If no page number is set, the default page to 1
}
//eerst wordt de query opgehaald om te bepalen hoeveel pagina's nodig zullen zijn.
$sql = "SELECT productnaam LIKE '".$invoer."' OR merk LIKE '".$invoer."'
FROM product
ORDER BY productnaam"; // dit is dus de query om de producten op te halen
$query = mysql_query($sql) or die (mysql_error()."<br>in file ".__FILE__." on line ".__LINE__); // als de query niet uitgevoerd wordt geeft die een foutmelding met bestandsnaam en regelnummer
$num = mysql_num_rows($query); // Het aantal producten
$Limit = 6; //Number of results per page
$NumberOfPages = ceil($Num/$Limit);
$sql = mysql_query("SELECT * FROM product WHERE productnaam LIKE '%$invoer%' OR merk='".$invoer."' AND Categorie_Nummer = 1 ORDER BY productnaam LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());
$num = mysql_num_rows($sql);
Upvotes: 0
Views: 95
Reputation: 270607
Your table (if that is the full table) has no records where merk = 'sigarillos' OR mark = 'aansteker'
so the query will never return rows for those searches. Perhaps you intend to search `productnaam as well.
Also, please remember to use mysql_real_escape_string()
to filter $invoer
against SQL injection:
$invoer = mysql_real_escape_string($invoer);
To search for productnaam
as well, change your query to the one below. To get your error message showing, use mysql_num_rows()
after the query above, rather than before it.
$sql = mysql_query("SELECT * FROM product WHERE productnaam LIKE '%$invoer%' OR merk='".$invoer."' AND Categorie_Nummer = 1 ORDER BY productnaam LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());
$num = mysql_num_rows($sql);
Upvotes: 2