dongtangze6393 2016-02-07 20:19
浏览 53
已采纳

无论输入如何,搜索引擎的查询都会返回相同的结果

I'm writing a script for a search engine where all fields need to be able to have information submitted by themselves, or in combination with other fields. The fields are Name, City, State, ZIP, Country, Category_1. The code is below:

"SELECT * FROM table WHERE Name LIKE '%$get_name%' 
OR City LIKE '%$get_city%' 
OR State LIKE '%$get_state%' 
OR ZIP LIKE '%$get_zip%' 
OR Country LIKE '%$get_country%' 
OR Category_1 LIKE '%$get_category1%' 
LIMIT 0, 10";

If I echo all the variables, from the inputs, they all come out correct. However, the query brings up the same results regardless of what the user input was. Any suggestions? It seems to be a problem with the MYSQL query itself.

  • 写回答

1条回答 默认 最新

  • du248227 2016-02-07 20:24
    关注

    if any of the variables is empty you will get all results. its the same as Name LIKE '%%' = all results.

    You can use it this:

    $fields = array(
        'Name'  => $get_name,
        'City'  => $get_city,
        'State' => $get_state,
        'ZIP'   => $get_zip,
        'Country'   => $get_country,
        'Category_1'    => $get_category1,
    );
    
    $query = "";
    foreach($fields as $k => $data) {
        if(trim($data) != '') {
            $query .= ($query ? ' OR ' : 'WHERE ') . "$k LIKE '%% $data'";
        }
    }
    
    if($query) {
        $query = "SELECT * FROM table $query LIMIT 0, 10";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向