簡體   English   中英

具有多個字段的PHP和MYSQL動態搜索無法具有日期范圍

[英]PHP & MYSQL Dynamic Search with multiple fields fails having date range

我已經使用PHP和MySQL構建了動態搜索。 一切正常,代碼可以同時搜索多個值。

$query="";
$query .=" select * from customers where 1 ";  

if(strlen($adv_s_city)>0){  
    $query .=" and  city = '$adv_s_city'  ";
}             
if(strlen($adv_s_personalIdType)>0)
{             
    $query .=" and  pesonal_identity_type = '$adv_s_personalIdType'  ";
}
if(strlen(isset($_POST['adv_s_gender'])) > 0)
{                 
    $query .=" and  gender = '$adv_s_gender'  ";
}

但是,當我還向它添加下面的代碼並指定兩個日期之間的日期范圍時,它會給我這兩個日期之間的值,但是搜索其他字段將無效或者它會跳過代碼。 什么可能出錯?!

if(strlen($adv_s_FromDate)>0 && strlen($adv_s_ToDate)>0)
{
    $query .=" and  (date between '$adv_s_FromDate' and '$adv_s_ToDate')  ";
}

您的代碼生成的查詢類型對我來說很好。 我使用的是mysql 5.7.13版。 可能錯誤在生成查詢的代碼中。 嘗試打印完整的查詢並在mysql中運行它以獲得清晰的視圖。 也許某些變量是空的,因此這些條件不會添加到查詢中。

我在我的數據庫上使用了類似的查詢,如下所示:

SELECT * FROM database.subsection where 1 and subsection_type='textbox' and subsection_position=2 and (creation_time between '2016-09-29 18:46:41' and '2016-09-30 10:56:40');

此查詢在我的數據庫中給出了正確的結果。 請檢查你的代碼,也許你錯過了那里的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM