我做了一个搜索功能,并让它工作,但我有一个问题: 如果我输入1个单词(让我们说"测试")它工作正常并找到包含" test"的行。但是,如果我输入"测试别的东西"它找不到任何东西。
如何让脚本搜索$ _POST中的每个单词['搜索'] ??
这是我的代码:
$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 ></button></a> </div>';
}
编辑:感谢所有输入,我得到它的工作:)
答案 0 :(得分:1)
如果您使用ctype_alpha()
过滤search_term,那么您将获得$ result_count = 0. ctype_alpha()
仅允许[a-zA-Z]。放入空间会使返回错误。