简体   繁体   English

使用多个值搜索表单查询 - PHP / MYSQL

[英]Search form query with multiple values - PHP / MYSQL

I'm having a little trouble with a search form I've been creating the functionality for. 我在使用搜索表单时遇到了一些麻烦,我一直在创建功能。 I basically want a form (on whatever page) to go to this page then list the relevant rows from my database. 我基本上想要一个表单(在任何页面上)转到此页面,然后列出我的数据库中的相关行。 My problem is that the form has both a text field and a select field (for name and categories) and I've been unable to create the functionality for having these two values search the database together. 我的问题是表单有文本字段和选择字段(用于名称和类别),我无法创建使这两个值一起搜索数据库的功能。

So heres what I want to happen: When you only type in the name and not the category, it will display from just the name, vise versa for the category and no name; 这就是我想要发生的事情:当你只输入名称而不是类别时,它将仅显示名称,反之亦然,类别和名称; then when both together it only displays rows with both of them in. 然后当它们在一起时它只显示两个都在的行。

Heres what I have so far: 以下是我到目前为止的情况:

// 2. Create variables to store values
if(!$_GET['search-category'] == "") {
    $searchName = $_GET['search-name'];
}

if(!$_GET['search-category'] == "select-your-category") {
    $searchCat = $_GET['search-category'];
}



// 2. Create the query for the stored value. Matching it against the name, summary and sub type of my item.
$mainSearch = "SELECT attraction.*, type.type_name, sub_type.sub_type_name ";
$mainSearch .= "FROM attraction ";
$mainSearch .= "INNER JOIN sub_type ON attraction.sub_type = sub_type.sub_type_id ";
$mainSearch .= "INNER JOIN type ON attraction.type = type.type_id ";
$mainSearch .= "WHERE attraction.name LIKE '%" . $searchName . "%' AND (sub_type.sub_type_name LIKE '%" . $searchCat . "%' )";
$mainSearch .= "ORDER BY sub_type_name ASC";

// 2. run query
$result2 = $con->query($mainSearch);
if (!$result2) {
    die('Query error: ' . mysqli_error($result2));
}

I'd refactor the code to something like - 我将代码重构为类似的东西 -

foreach( $_GET['filters'] as $fname => $fval ) {

    if( !$fval ) continue;

    $where[] = "$fname LIKE '%{$fval}%'";
}

You need to include only those inputs that are non-empty in the query. 您只需要包含查询中非空的输入。 Also you will need to address security issues like escaping inputs etc. 此外,您还需要解决安全问题,例如转移输入等。

You can just check that the relevant values aren't empty: 您可以检查相关值是否为空:

// 2. Create the query for the stored value. 
// Matching it against the name, summary and sub type of my item.
$mainSearch = "SELECT attraction.*, type.type_name, sub_type.sub_type_name ";
$mainSearch .= "FROM attraction ";
$mainSearch .= "INNER JOIN sub_type ON attraction.sub_type = sub_type.sub_type_id ";
$mainSearch .= "INNER JOIN type ON attraction.type = type.type_id ";
$mainSearch .= "WHERE ";
if ($searchName) {
    $mainSearch .= "attraction.name LIKE '%" . $searchName . "%'";
    if ($searchCat) {
        $mainSearch .= " AND ";
    }
}
if ($searchCat) {
    $mainSearch .= "sub_type.sub_type_name LIKE '%" . $searchCat . "%'"
}

$mainSearch .= "ORDER BY sub_type_name ASC";

// Double check that at least one of the search criteria is filled:
if (!$searchName && !$searchCat) {
    die("Must supply either name search or category search");
}

What you can do is that declare a variable called $search_condition and based on whether $searchName or $searchCat is null or not assign value to $search_condition 您可以做的是声明一个名为$ search_condition的变量,并根据$ searchName或$ searchCat是否为null或不为$ search_condition赋值

For eg 例如

if (isset($searchName ) || !is_empty($searchName ))
{
   $search_condition = "WHERE attraction.name LIKE '%" . $searchName;
}
if (isset($searchCat ) || !is_empty($searchCat ))
{
   $search_condition = "sub_type.sub_type_name LIKE '%" . $searchCat . "%'";
}
if ((isset($searchName ) || !is_empty($searchName )) && (isset($searchCat ) || !is_empty($searchCat )))
{
   $search_condition = "WHERE attraction.name LIKE '%" . $searchName . "%' AND (sub_type.sub_type_name LIKE '%" . $searchCat . "%' )";
} 

Hope this might help you 希望这可能对你有所帮助

Thanks 谢谢

This is a comment, but I want to take advantage of formatting options... 这是一个评论,但我想利用格式化选项......

You know, you can rewrite that this way... 你知道,你可以这样改写......

// 2. Create the query for the stored value. Matching it against the name, summary and sub type of my item.

$mainSearch = "
SELECT a.*
     , t.type_name
     , s.sub_type_name 
  FROM attraction a 
  JOIN sub_type s
    ON a.sub_type = s.sub_type_id 
  JOIN type t
    ON a.type = t.type_id 
 WHERE a.name LIKE '%$searchName%' 
   AND s.sub_type_name LIKE '%$searchCat%' 
 ORDER 
    BY s.sub_type_name ASC;
    ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM