简体   繁体   English

具有多个字段的PHP和MYSQL动态搜索无法具有日期范围

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

I have built a dynamic search using PHP & MySQL. 我已经使用PHP和MySQL构建了动态搜索。 Everything is working fine and the code can search for multiple values at the same time. 一切正常,代码可以同时搜索多个值。

$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'  ";
}

However, when I also add below code to it and specify a date range between two dates, it is giving me the values between these two dates, but then searching for other fields will not work or it skips above codes. 但是,当我还向它添加下面的代码并指定两个日期之间的日期范围时,它会给我这两个日期之间的值,但是搜索其他字段将无效或者它会跳过代码。 What could have gone wrong?! 什么可能出错?!

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

The type of query that your code is generating is working fine for me. 您的代码生成的查询类型对我来说很好。 I am using mysql version 5.7.13. 我使用的是mysql 5.7.13版。 Maybe the error is in the code that is generating the query. 可能错误在生成查询的代码中。 Try to print the complete query and run it in mysql to get a clear view. 尝试打印完整的查询并在mysql中运行它以获得清晰的视图。 Maybe some variables are coming out to be empty so those conditions are not added in the query. 也许某些变量是空的,因此这些条件不会添加到查询中。

I used a similar query on my database shown below : 我在我的数据库上使用了类似的查询,如下所示:

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');

This query is giving me the correct results on my database. 此查询在我的数据库中给出了正确的结果。 Please, check you code maybe you are missing something there. 请检查你的代码,也许你错过了那里的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM