have a website search script that searches my database then displays the search result like this :
item name
item number
view details for this item
scripts works fine i have one issue i have to fix on it.
Currently when a user enters a search that is less than 2 characters it displays a error to user that tells them "Search terms must be longer than 2 characters"
I have been trying to do the same thing with if a user enters in certain words by themselfs ect ( white, wheat, rye, bread) the database is for bread products and these 4 searches will result in the whole database to come up in the search results
Here is the code that i am using
<?php
require_once ('config.inc.php'); // error handler file
require_once ('connection file'); // Connect to the database.
// Set up our error check and result check array
$error = array();
$results = array();
// First check if a form was submitted.
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 2) {
$error[] = "Search terms must be longer than 2 characters.";
if (strlen($searchTerms) = wheat, white, rye, bread) {
$error[] = "Your search terms are to broad, please try your search again.";
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT link, keywords FROM products WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['keywords'])?"`keywords` LIKE '%{$searchTermDB}%'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
if (count($types) < 1)
$types[] = "`keywords` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `keywords`";
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The search term provided {$searchTerms} yielded no results.";
}else {
$results = array(); // the result array
$i = 1;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = " {$row['link']}<br />";
}
}
}
}
function removeEmpty($var) {
return (!empty($var));
}
?>
<html>
<title>Search</title>
<body>
<div align="center"><?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />", $error) . "</span><br /><br />":""; ?>
</div>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm">
<div align="center">What are you looking for:
<input name="search" type="text" value="<?php echo isset($searchTerms)?htmlspecialchars($searchTerms):''; ?>" size="50" /><input type="submit" name="submit" value="Search" />
</div>
</form>
<div align="center"><?php echo (count($results) > 0)?"Your search: {$searchTerms} returned:<br /><br />" . implode("", $results):""; ?></div>
</body>
</html>
this is the lines i am having the issue with
if (strlen($searchTerms) < 2) {
$error[] = "Search terms must be longer than 2 characters."; // this one works
if (strlen($searchTerms) = wheat, white, rye, bread) {
$error[] = "Your search terms are to broad, please try your search again."; // this one doesn't
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}