【发布时间】:2015-02-24 03:24:54
【问题描述】:
// Check and set search
if($_POST['searchQuery'] !== "null"){
$search = $_POST['searchQuery'];
$search = explode(' ', $search);
//long words are more than 4
$longwords = '';
$shortwords = '';
$searchCount = count($search);
foreach ($search as $word) {
$word = trimplural($word);
if ($searchCount > 1){
if (strlen($word) > 3) {
if (!in_array($word,array('sale','brand','lots'))){
$longwords.=' +'.$word;
} //check for words
}else{ //else 3 letters
if (strlen($word) == 3) {
if (!in_array($word,array('and','the','him','her','for','new','you'))){
$shortwords.= " OR (fname LIKE '%$word%' OR lname LIKE '%$word%') ";
} //search for words
}//strlen == 3
}
}else{//else searchcount == 1
if (!in_array($word,array('and','the','him','her','for','new','you'))){
$shortwords.= " OR (fname LIKE '%$word%' OR lname LIKE '%$word%') ";
}
}
}
}else{
$search = null;
}
Sql:
$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:longwords IN BOOLEAN MODE) $shortwords LIMIT " . $postnumbers . " OFFSET ".$offset;
$q1 = $conn->prepare($sql) or die("failed!");
$q1->bindParam(':uniid', $uniid, PDO::PARAM_STR);
$q1->bindParam(':longwords', $longwords, PDO::PARAM_STR);
$q1->execute();
我有一个使用上面显示的代码生成的搜索查询,我想结合使用 mysql 全文搜索和 LIKE 查询。为了做到这一点,我将 SQL 查询的一部分添加为变量 $shortwords 以使 LIKE 部分工作,但是,我知道这不是最佳选择,因为 sql 注入。
在实施到 SQL 之前,如何使这个查询“更安全”或清理输出?
【问题讨论】:
标签: php sql pdo sql-injection