php强化搜索引擎

fivyi3re  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(359)

我有这个搜索选项,它在其中查找表单请求的变量的标题和类别引用。

//This could be Reference or Product Name
$name = mysqli_real_escape_string($con, sanitize($_GET['search']));

//this could be a specific category or Empty(used to reference number)
$category = mysqli_real_escape_string($con, sanitize($_GET['category']));
$x = 0; 
$q = str_replace(array("\\",";"), "", $name);  // remove ALL backslashes & remove ALL ";" -> for sql security: no (simple) injection of commands
$q = trim($q);
$search_exploded = explode(" ", $q);

foreach($search_exploded as $search_each ) { 

$x++; 

   if($x == 1) { 
         $wherearr[]= "ads_title LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0"; 
   } else {     
         $wherearr[]= "ads_title LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0"; 
   }

}

//$wherearr[] is used to create a new variable $construct to insert to SQL like "Select * from Where $construct"

我希望人们能够搜索也由产品参考。
目标是,当用户插入引用时,它将自动转到产品。
添加此字段:

$wherearr[]= "ads_reference LIKE '%$search_each%' AND ads_active = 1 AND ads_end = 0";

如何使用我以前创建的窗体来完成它?

rnmwe5a2

rnmwe5a21#

我不确定我是否完全理解你的问题,但我会尽力的。
首先尝试从 GET 请求。
一些假设:
你曾经 AND category_id = '$category' 所以我想 $_GET['category'] 检索类别id。
提取时 $_GET['search'] 你得到的名字列表用空格隔开。如果你有这样的名字 foo 以及 bar 这个 $_GET['search'] 将是“foo bar”
如果 category_id 是空的我想 $name 包含引用
现在尝试以下代码:

$name = mysqli_real_escape_string($con, sanitize($_GET['search']));
$category_id = mysqli_real_escape_string($con, sanitize($_GET['category']));
$q = str_replace(array("\\",";"), "", $name); 

$search_exploded = explode(" ", trim($q));
$wherearr = array();
$querySufix = " AND ads_active = 1 AND ads_end = 0";

foreach($search_exploded as $search_each) {
    if ($category_id)
        $wherearr[]= "ads_title LIKE '%$search_each%' AND category_id = '$category_id '" . $querySufix;
    else 
        $wherearr[]= "ads_reference LIKE '%$search_each%'". $querySufix;
}

相关问题