作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个“搜索”选项,它在其中查找表格所请求变量的标题和类别。
//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";
如何使用我之前创建的表单来完成?
最佳答案
不确定我是否完全理解你的问题,但我会尝试。
首先尝试从 GET
请求中提取所有你想要的数据。
一些假设:
AND category_id = '$category'
所以我假设 $_GET['category']
检索 category_id。 $_GET['search']
时,您会得到由空格分隔的名称列表。因此,如果您有 foo
和 bar
这样的名称,则 $_GET['search']
将是“foo bar”category_id
为空,我假设 $name
包含 Reference 现在试试这段代码:
$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;
}
关于PHP Reforce 搜索引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52136702/
我有这个“搜索”选项,它在其中查找表格所请求变量的标题和类别。 //This could be Reference or Product Name $name = mysqli_real_escape
我是一名优秀的程序员,十分优秀!