【问题标题】:select table filter by querystring php通过查询字符串 php 选择表过滤器
【发布时间】:2011-05-10 18:08:02
【问题描述】:

好的,所以我已经尝试了一段时间来让它工作,但必须有一个比我想的更好的解决方案。我对 php/mysql 还很陌生,所以不确定如何执行以下操作:

我有一个包含国家、州、城市下拉菜单的搜索框 现在,如果用户只选择国家并点击搜索,则需要仅按国家过滤选择并显示其他所有内容。

if(!empty($_REQUEST['city']))
    $city = $_REQUEST['city'];
else
    $city= "%";

if(!empty($_REQUEST['state']))
    $state= $_REQUEST['state'];
else
    $state= "%";

if(!empty($_REQUEST['country']))
    $country= $_REQUEST['country'];

select * from table where country = $country and state = $state and city = $city

问题在于这些列是整数,所以我不能使用“%”来过滤它。我希望我能够解释它,任何帮助都非常受欢迎。提前致谢

【问题讨论】:

    标签: php mysql select filter query-string


    【解决方案1】:
    1. 如果您不想限制列,只需在查询中省略它即可
    2. 永远不要将 $_REQUEST 中的字符串直接插入到查询字符串中——典型的 SQL 注入缺陷。
    3. 您可能希望实施某种限制,以免查询返回数据库中的每一个结果。

    示例:

    <?php
    $conditions = array();
    
    if(!empty($_REQUEST['city']))
        $conditions[] = "city = " . mysql_real_escape_string($_REQUEST['city']);
    
    if(!empty($_REQUEST['state']))
        $conditions[] = "state = " . mysql_real_escape_string($_REQUEST['state']);
    
    if(!empty($_REQUEST['country']))
        $conditions[] = "country = " . mysql_real_escape_string($_REQUEST['country']);
    
    $sql = 'select * from table ';
    if(!empty($conditions))
        $sql .= ' where '. implode(' AND ', $conditions);
    $sql .= ' LIMIT 1000';
    

    【讨论】:

    • 我唯一的评论是使用 (int) 而不是 mysql_real_escape_string。他说这些字段是整数。除此之外,似乎我们有相同的想法;-)
    • @Frank Farmer,很好,你完全打败了我,希望我能打字更快...... :) @PENDO,你也有一个好方法,虽然我认为,我可能会错误,在处理整数和 MySQL 时,您必须省略值本身的单引号,单引号用于字符和字符串,而不是整数。
    • 这只是我多年来习惯的东西。它工作得很好,但你的观点被接受了,我必须“不教”自己引用整数。
    • 在 mysql 中引用整数并没有太大的伤害(尽管我也没有立即意识到任何好处); mysql 根据需要进行强制转换(在这种情况下它甚至会发出警告吗?)。但是,您必须对其他一些数据库更加小心——postgres 8.3 及更高版本将拒绝在与 int 列的比较中自动转换带引号的 int。
    • 这很好用,但出于某种奇怪的原因,即使我过滤它,它也会返回所有城市。
    【解决方案2】:
    $where = array();
    if(!empty($_REQUEST['city'])) $where[] = "city = '".(int)$_REQUEST['city']."'";
    if(!empty($_REQUEST['state'])) $where[] = "state = '".(int)$_REQUEST['state']."'";
    if(!empty($_REQUEST['country'])) $where[] = "country = '".(int)$_REQUEST['country']."'";
    
    $wherestring = if(count($where) != 0) ? " WHERE ".implode(' AND ', $where) : "" ;
    
    $query = "SELECT * FROM table".$wherestring;
    

    【讨论】:

    • 看来我来得太晚了:D
    【解决方案3】:

    您可能需要考虑编写多个查询字符串,一个仅代表国家,一个代表州和国家,一个代表城市、州和国家。或者,您可以根据必须使用的不同参数组合查询字符串。

    例子:

    if(isset() || isset() || isset() ) //make sure at least one is set
    {
      $query_string = "SELECT * FROM table WHERE ";
    
      if(isset($_REQUEST['country']))
      {
          $country = $_REQUEST['country'];
          $query_string .= " country = $country";
      }
    
      if(isset($_REQUEST['state']))
      {
          $state = $_REQUEST['state'];
          $query_string .= " state = $state";
      }
    
      if(isset($_REQUEST['city']))
      {
          $city = $_REQUEST['city'];
          $query_string .= " city = $city";
      }
    }
    else
    {
        //Else, if none are set, just select all the entries if no specifications were made
        $query_string = "SELECT * FROM table";
    }
    
      //Then run your query...
    

    因此,在英语中,您要做的第一件事是检查参数,确保在尝试将空变量连接在一起之前有一些东西可以使用。 然后你创建基本查询字符串(只要我们有参数)并让它保持开放式,以便我们可以添加你需要的任何参数。 接下来检查每个参数,如果已设置,则将该参数连接到查询字符串的末尾。 最后通过将查询发送到 SQL 服务器来处理查询。

    祝你好运!

    h

    【讨论】:

      【解决方案4】:

      这是我的建议。

      我给你一个答案,即使你已经有了三个。我在想我的可能在代码眼上更容易。

      1. 不要不要使用原始的$_REQUEST 值,因为用户可能会通过向数据库提供虚假的$_REQUEST 数据来毒害您的数据库。虽然可能有更好的方法,但请记住命令“mysql_real_escape_string($string)”。
      2. 我见过的解决这个问题的常用方法写在下面。 (基本上是内爆的想法。弗兰克·法默(Frank Farmer)也做到了。)

      -

      $__searchWheres = array(); //Where we'll store each requirement used later
      foreach( array('city','state','country') as $_searchOption) {
         if ( ! empty( $_REQUEST[$_searchOption] ) ) {
             $__searchWheres[] = $_searchOption . '= "' . mysql_real_escape_string( $_REQUEST[$_searchOption] ) . '"';
         }
      }
      $__query = 'select * from table' . (count($__searchWheres) > 0 ? ' WHERE ' . implode(' AND ',$__searchWheres) : '');  //Implode idea also used by Frank Farmer
      //Select from the table, but only add the 'WHERE' key and where data if we have it.
      mysql_query($__query);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-19
        • 1970-01-01
        • 2011-04-21
        • 1970-01-01
        • 2015-08-19
        • 1970-01-01
        • 2012-05-22
        • 1970-01-01
        相关资源
        最近更新 更多