【问题标题】:Criteria Search based on TextField and a List Menu基于 TextField 和列表菜单的条件搜索
【发布时间】:2011-02-28 16:08:54
【问题描述】:

我正在尝试进行搜索,用户可以根据某些条件搜索某些场所(夜总会)。

到目前为止,我已经制作了一个搜索框,用户可以在其中输入俱乐部的名称和一个列表菜单来选择位置。

下拉列表菜单(从数据库动态生成)如下:

地点:

Select
Westlands
Eastleigh
Langata
Kamukunji
Dagoretti
Starehe 
Makadara

我想让它这样,如果用户将文本字段留空并选择 westlands 并点击搜索按钮,则会显示 westlands 的所有俱乐部。 This is working fine.

此外,用户可以在文本框中输入内容并将列表菜单保留为默认选择,然后将显示匹配结果。 This is working fine too.

我还希望当用户在文本框中输入“P”并选择 Westlands 时,所有以字母“P”开头的俱乐部都会显示出来。 This is not working.

我的代码:

//get textfield search value
$search_value = "-1";
if (isset($_POST['event_search'])) 
{
  $search_value = $_POST['event_search'];
  $search_value = mysql_real_escape_string($search_value);
}

//get location search value
$location_search_value = "-1";
if (isset($_POST['location'])) 
{
  $location_search_value = $_POST['location'];
  $location_search_value = mysql_real_escape_string($location_search_value);
}

//query establishments table
mysql_select_db($database_connections, $connections);
$query_establishment = "SELECT establishment_id, establishment_thumb_url, establishment_name, establishment_pricing, location_name
FROM establishment JOIN location ON establishment.location_id = location.location_id WHERE (establishment_name LIKE '".$search_value."%' 
AND establishment.location_id = '$location_search_value') OR (establishment_name LIKE '".$search_value."%' 
OR establishment.location_id = '$location_search_value')";
$establishment = mysql_query($query_establishment, $connections) or die(mysql_error());
$totalRows_establishment = mysql_num_rows($establishment);

截至目前,当我在文本框中输入“P”并选择 westlands 时,所有以字母 P 开头的俱乐部都会出现,无论位置如何。我该如何解决这个问题?

非常感谢任何帮助。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    你的 where 子句有些奇怪。

    如果你测试:(A AND B) OR (A OR B)

    A => a.establishment_name LIKE '".$search_value."'
    B => a.location_id = '".$location_search_value."'
    

    如果 A 为真,则 b 不需要为真。这解释了您的第三个示例不起作用。我认为您应该测试您的价值并根据您的解释创建正确的 WHERE 子句。

    if($search_value != "" && $location_search_value == "") {
        $where = "a.establishment_name LIKE '".$search_value."'";
    } else if ($search_value == "" && $location_search_value != "") {
        $where = "a.location_id = '".$location_search_value."'";
    } else {
        $where = "(a.establishment_name LIKE '".$search_value."' AND a.location_id = '".$location_search_value."')";
    }
    $query = "SELECT a.*, b.location_name ".
             "FROM establishment a ".
             "JOIN location b ON a.location_id = b.location_id ".
             "WHERE ".$where; 
    

    【讨论】:

    • 啊,我明白我的错误了。我的 (A AND B) OR (A OR B) 推理有点错误,哈哈。让我试试你的答案,然后回复你。
    【解决方案2】:

    查询中有错误。您正确插入了$search_value,但是您在第二个变量上使用了没有. 的单引号。试试这个:

    $query = "SELECT a.*, b.location_name
      FROM establishment a JOIN location b ON a.location_id = b.location_id
      WHERE (a.establishment_name LIKE '".$search_value."' AND a.location_id = '".$location_search_value."')
      OR (a.establishment_name LIKE '".$search_value."' OR a.location_id = '".$location_search_value."')";
    

    【讨论】:

    • 感谢您的回复。但是当我在文本字段中输入内容并选择一个位置时,我仍然无法仅获得特定位置内的俱乐部。我认为这是查询中的逻辑错误。
    • 抱歉,我在此查询中找不到任何逻辑错误。如果表结构和变量正确,它应该可以工作。
    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多