0

Ive made a search function and got it working, but I have 1 problem: If I input 1 word (lets say "test") it works fine and finds the row containing "test". But if I input "test something else" it dosent find anything.

How do I make the script search for each individual word in $_POST['search'] ??

Here's my code:

$itemOutput = '';

$dbhandle = mysql_connect('host', 'name', 'pass') or die("Unable to connect to MySQL");
$selected = mysql_select_db('my_website_db', $dbhandle) or die("Could not select database");

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

if (!isset($search_term) || $search_term == '' || $search_term == ' ' || !ctype_alpha($search_term)) { $result_count = "0"; } else {

$fetch_items = mysql_query("SELECT * FROM products WHERE name LIKE '%{$search_term}%' OR short_info LIKE '%{$search_term}%'");

while($shop_items = mysql_fetch_array($fetch_items)) {
$item_id = $shop_items['id'];
$item_name = $shop_items['name'];
$item_price = $shop_items['price'];
$item_image = $shop_items['image']; }

$itemOutput .= '<div class="item-box"> <img src="'.$item_image.'" class="item-image" />';
$itemOutput .= '<div class="item-price"> <a style="padding-left: 15px; font-size: 14px; padding-top: 3px;">Kun </a> <a><b>'.$item_price.' kr.</b></a> </div> <br>';
$itemOutput .= '<div class="item-name"> <a>'.$item_name.'</a> </div> <br>';
$itemOutput .= '<a href="vare.php?id='.$item_id.'"><button class="item-button">Køb her &nbsp;></button></a> </div>';
}

EDIT: Thanks to all the inputs, I just got it to work :)

10
  • Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think. Commented May 8, 2015 at 19:46
  • 1
    Split up the terms and append each word to it's own like grouping dynamically. e.g. $query .= '(name LIKE '%{$search_term}%' OR short_info LIKE '%{$search_term}%') or '; //strip the last "or" as well keep the escaping in or better use a driver that supports prepared statements. Commented May 8, 2015 at 19:47
  • Each individual word or each word in that order? If in that order simply replace space with if any order as @chris85 suggests Commented May 8, 2015 at 19:50
  • @xQbert should be each individual word in $_POST['search'] Commented May 8, 2015 at 19:57
  • Consider using explode to split the string into an array and then concatenate the search criteria using or statements all wrapped together loop through the array and build or statements (slow as heck though and gets worse for each word added due to multiple OR statements.... ) maybe you can use a full text search match against syntax instead Commented May 8, 2015 at 20:06

1 Answer 1

1

If you filter the search_term using ctype_alpha(), then you will get $result_count = 0. ctype_alpha() only allows [a-zA-Z]. Putting in a space will make that return false.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.