【发布时间】:2016-03-30 02:54:59
【问题描述】:
<?$search=$_POST['search'];
$query = $pdo->prepare("select * from tag where tag1 LIKE '%$search%' OR tag2 LIKE '%$search%' LIMIT 0 , 10");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo "Search found :<br/>";
while ($results = $query->fetch()) {
echo "$".$results['name'];
echo "</td></tr>";
}
echo "</table>";
} else {
echo 'Nothing found';
}
?>
<form action="" method="post">
Search: <input type="text" name="search" placeholder=" Search here ... "/>
<input type="submit" value="Submit" />
</form>
我知道有很多类似的问题,但我仍然无法弄清楚。如果有人有时间,请解释我如何将explode 添加到我的搜索中,以便我可以使用超过 1 个单词进行搜索?
非常感谢您的时间。
如果我输入 1 个单词,这个脚本会搜索,在我的 case 标签中。但是如果我输入 2 个单词,它将返回 0 个结果。
【问题讨论】:
-
按分隔符展开搜索字符串
-
sql注入高风险:
$search = '%'.$search.'%'; $query = $pdo->prepare("select * from tag where tag1 LIKE :strings OR tag2 LIKE :strings LIMIT 0 , 10"); $query->bindParam(':strings', $search, PDO::PARAM_STR); -
仅供参考,您对 SQL 注入不是安全的。通过在此处嵌入用户输入:
%$search%,您允许用户直接操作您的 SQL 语句。您对bindValue的使用不正确。考虑像这样更改您的声明:$query = $pdo->prepare("select * from tag where tag1 LIKE '%:search1%' OR tag2 LIKE '%:search2%' LIMIT 0 , 10"); $query->bindValue(":search1", $search, PDO::PARAM_STR); $query->bindValue(":search2", $search, PDO::PARAM_STR);