PHP,sql 动态查询可能与 LIKE 和 OR 和 AND 一起使用,但无法以正确的方式工作

标签 php mysql search operators combinations

我有一个搜索表单(搜索特性、房地产):

HTML: 省略标签

 <input type="text" name="keywords" placeholder="keywords"/>

<select name="status" id="select-property-status" class="search-select">
    <option value="" selected="selected">Tous</option>
    <option value="1">Sale</option>
    <option value="0">Rent</option>
</select>

<select name="type" id="select-property-type" class="search-select">
    <option value="" selected="selected">Tous</option>
    <option value="appartement">Appartement</option>
    <option value="duplex">Duplex</option>
    <option value="studio">Studio</option>
    <option value="villa">Villa</option>
</select>

<select name="emplacement" id="select-property-emplacement" class="search-select">
    <option value="" selected="selected">Tous</option>
    <option value="La Goulette">La Goulette</option>
    <option value="Gammarth">Gammarth</option>
    <option value="La Marsa">La Marsa</option>
    <option value="El Mourouj">El Mourouj</option>
</select>

  <input name="surface_min" type="text" placeholder="min" value=""  />
  <input name="surface_max" type="text" placeholder="max" value=""  />

  <input name="rooms_min" type="text" placeholder="min" value=""  />
  <input name="rooms_max" type="text" placeholder="max" value=""  />

  <input name="price_min" type="text" placeholder="Prix min" value=""  />
  <input name="price_max" type="text" placeholder="Prix max" value=""  />

  <input type="hidden" name="search_form" value="True" required="required"/>
  <input type="submit" name="submit" value="Search"  class="search-button" />       

现在search-results.php:

    // Saving search terms in variables 
    $keyword = strtolower(htmlspecialchars($_POST['keywords'])); // string from text field
    $status = htmlspecialchars($_POST['status']); // 0 or 1
    $type = htmlspecialchars($_POST['type']); // string value from option
    $emplacement = htmlspecialchars($_POST['emplacement']); // string value from option
    $surface_min = htmlspecialchars($_POST['surface_min']); // integer from text field
    $surface_max = htmlspecialchars($_POST['surface_max']);// integer from text field
    $chambres_min = htmlspecialchars($_POST['rooms_min']);// integer from text field
    $chambres_max = htmlspecialchars($_POST['rooms_max']);// integer from text field
    $prix_min = htmlspecialchars($_POST['price_min']);// integer from text field
    $prix_max = htmlspecialchars($_POST['price_max']);// integer from text field

    // create an empty array
    $where_array = array();

    // if user type in or choose an option in the search form 
    // > create a string with its value and put it in $where_array
    if($keyword != '') $where_array[] = ' LOWER(prop_title) LIKE "%'.$keyword.'%" OR LOWER(prop_desc) LIKE "%'.$keyword.'%"';
    if($status != '') $where_array[] = 'prop_status = '.$status.'';
    if($type != '') $where_array[] = 'prop_type = "'.$type.'"';
    if($emplacement != '') $where_array[] = 'prop_place = "'.$emplacement.'"';
    if($surface_min != '') $where_array[] = 'prop_surface >= '.$surface_min.'';
    if($surface_max != '') $where_array[] = 'prop_surface <= '.$surface_max.'';
    if($rooms_min != '') $where_array[] = 'prop_rooms >= '.$rooms_min.'';
    if($rooms_max != '') $where_array[] = 'prop_rooms <= '.$rooms_max.'';
    if($price_min != '') $where_array[] = 'prop_price >= '.$price_min.'';
    if($price_max != '') $where_array[] = 'prop_price <= '.$price_max.'';

    // join all the array elements (the strings) saved in $where_array by the string : AND 
    $where_string = implode(" AND ", $where_array);

   // the sql query        
    $search = "SELECT * FROM proprietes WHERE $where_string";
    $search = $db->query($search);

    // if the query exists 
    if($search)
    {
        // loop through all the properies that matches the WHERE conditions
        foreach($search as $search_result)
        {
            // echo the title of the property
            echo htmlspecialchars($search_result['prop_title']);
        }
    }
    else
    {
        // which one is right ?
        echo 'Error occured !';
        echo 'No property found !';
    }

问题

单独选择时,所有搜索字段都可以正常工作,所有组合都可以正常工作,除了:
关键字和状态字段,从属性表返回不应该的数据:我在文本字段中输入“别墅”一词,然后在选择框中仅选择“待售”(销售 = 1,租金 = 0),但结果给出我所有别墅出售和出租!我使用 $search->debugDumpParams(); 来回显:

SELECT * FROM proprietes WHERE LOWER(prop_title) LIKE "%villa%"OR LOWER(prop_desc) LIKE "%villa%"AND prop_status = 1

我认为我的逻辑和语法都是错误的!有什么帮助吗?

谢谢。

最佳答案

我想如果你把这行代码改成

if($keyword != '') 
   $where_array[] = ' (LOWER(prop_title) LIKE "%'.$keyword.'%" OR LOWER(prop_desc) LIKE "%'.$keyword.'%")';

它将纠正您的查询。

如果您使用双引号,那么您就不需要所有 . 连接,代码也可能会更具可读性,并且代码会变得更容易阅读。

if($keyword != '') 
    $where_array[] = " (LOWER(prop_title) LIKE '%$keyword%' OR LOWER(prop_desc) LIKE '%$keyword%')";

关于PHP,sql 动态查询可能与 LIKE 和 OR 和 AND 一起使用,但无法以正确的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551194/

相关文章:

php - 以编程方式有条件地向 Woocommerce 3 添加折扣

javascript - 如何使用 wp_localize_script 从另一个 php 文件传递​​参数

php - 使用 .htaccess 从 URL 中删除 PHP 文件扩展名

mysql - sql查询选择两列中具有相同id但不同值的记录

search - Elastic Search 父子数据搜索 Java API

php - 命名空间的用途是什么,我是否以正确的方式使用它?

sql - 自动计算非标准自增

mysql - 有没有办法将 backup.sql 文件导入到空数据库并跳过某些列

php - 如何在两个表的两个特定列中生成 '%search%'?

python - 如何匹配字符串中的信息