dsh1102
2019-01-18 18:09
浏览 271
已采纳

从数据库过滤信息时,SQL查询忽略WHERE条件

The following section of code is used to to filter data from an html page in real time from an SQL database using ajax. For some reason the condition in the SQL statement ignores the following condition samples_database.sample_storage != 'discarded' but it works in the second part of the else statement.

if(isset($_POST["query"])) {
    $search = mysqli_real_escape_string($conn, $_POST["query"]);
    $query .= "
    SELECT * FROM samples_database JOIN storage_database on samples_database.storage_location=storage_database.id
    WHERE samples_database.sample_storage != 'discarded'
    AND samples_database.env_sam_id LIKE '%".$search."%'
    OR samples_database.c_sam_id LIKE '%".$search."%' 
    OR samples_database.sample_type LIKE '%".$search."%' 
    OR storage_database.storage_name LIKE '%".$search."%'
    ORDER BY samples_database.env_sam_id;";
} else {
    $query .= "
    SELECT * FROM samples_database JOIN storage_database on samples_database.storage_location=storage_database.id WHERE sample_storage != 'discarded' ORDER BY samples_database.env_sam_id;";
}

Can anybody help me this this silly problem please?

图片转代码服务由CSDN问答提供 功能建议

以下代码部分用于使用ajax从SQL数据库实时过滤html页面中的数据 。 由于某种原因,SQL语句中的条件忽略以下条件 samples_database.sample_storage!='abandoned',但它适用于 else 语句的第二部分。

  if(isset($ _ POST [“query”])){
 $ search = mysqli_real_escape_string($ conn,$ _POST [“query”]); 
 $ query。=  “
 SELECT * FROM samples_database JOIN storage_database on samples_database.storage_location = storage_database.id 
 WHERE samples_database.sample_storage!='abandoned'
 AND samples_database.env_sam_id LIKE'%”。$ search。“%'
 OR samples_database。  c_sam_id LIKE'%'。$ search。“%'
 OR samples_database.sample_type LIKE'%”。$ search。“%'
 OR storage_database.storage_name LIKE'%”。$ search。“%'
 ORDER BY  samples_database.env_sam_id;“; 
} else {
 $ query。=”
 SELECT * FROM samples_database JOIN storage_database on samples_database.storage_location = storage_database.id WHERE sample_storage!='abandoned'ORDER BY samples_database.env_s  am_id;“; 
} 
   
 
 

有人可以帮我解决这个愚蠢的问题吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyan8267 2019-01-18 18:14
    已采纳

    There's an order of precedence: AND is calculated before OR:

    if(isset($_POST["query"])) {
        $search = mysqli_real_escape_string($conn, $_POST["query"]);
        $query .= "
        SELECT * FROM samples_database JOIN storage_database on samples_database.storage_location=storage_database.id
        WHERE samples_database.sample_storage != 'discarded'
        AND (samples_database.env_sam_id LIKE '%".$search."%' OR
             samples_database.c_sam_id LIKE '%".$search."%' OR
             samples_database.sample_type LIKE '%".$search."%' OR 
             storage_database.storage_name LIKE '%".$search."%')
        ORDER BY samples_database.env_sam_id;";
    } else {
        $query .= "
        SELECT * FROM samples_database JOIN storage_database on samples_database.storage_location=storage_database.id WHERE sample_storage != 'discarded' ORDER BY samples_database.env_sam_id;";
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dt3358 2019-01-18 18:17

    In the else part you have only 1 condition:

    sample_storage != 'discarded'
    

    so it can't be "ignored".
    But in the if part you have something like this:

    A and B or C or D or E...
    

    If one of C or D or E... is true then the result is true and you think that A is "ignored".
    So make use of parentheses around each part that suits your needs.
    maybe something like this:

    A and (B or C or D or E...)
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题