获取某人选择的2个日期之间的记录 php mysql
我正在尝试获取两个日期之间的记录。日期以“yyyy-mm-dd”形式输入输入类型=“文本”(例如,我会在文本输入中输入“2011-02-15”),然后发布到下一页,其中包含查询:
$start = $_POST["start"];
$end = $_POST["end"];
$sql_query = "SELECT * FROM actionlist WHERE date>='{$start} 00:00:00' AND date<='{$end} 23:59:59' ORDER BY id";
$result = mysql_query($sql_query);
表“actionlist”中的记录有一个名为“date”的字段,并且在使用“$date = date('Ymd H:i:s')”创建记录时自动输入该字段。
无论如何,我似乎无法选择任何记录。我需要以某种方式处理我的 $start 和 $end 变量吗?提前致谢。
I'm trying to get records between 2 dates. The dates are entered in the form "yyyy-mm-dd" into a input type="text" (so for example I would type "2011-02-15" into a text input) and then posted to the next page which has the query:
$start = $_POST["start"];
$end = $_POST["end"];
$sql_query = "SELECT * FROM actionlist WHERE date>='{$start} 00:00:00' AND date<='{$end} 23:59:59' ORDER BY id";
$result = mysql_query($sql_query);
The records in the table "actionlist" have a field called "date" and this is entered automatically when creating the record by using "$date = date('Y-m-d H:i:s')".
Anyway, I can't seem to get any records to be selected. Do I need to process my $start and $end variables somehow? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Between 来获取您想要的信息
$sql_query = "SELECT * FROM actionlist WHERE date BETWEEN '$start' AND '$end' ORDER BY id";
但您需要确保 start 和end 有效,转义等,防止sql注入等
You can use between to get the information you want
$sql_query = "SELECT * FROM actionlist WHERE date BETWEEN '$start' AND '$end' ORDER BY id";
But you need to make sure start and end are valid, and escaped,etc.. to prevent sql injection, and the like
编辑:trim()以防您的输入表单中有一些不需要的空格;然后进行一些卫生处理,并使用 BETWEEN ,正如每个人都在我之前建议的那样(该死的我写得太慢了......)
根据下面的评论编辑 2
Edit: the trim() in case you have some unwanted space in your input forms; a bit of sanitation then, and use BETWEEN, as everyone has suggested right before me (damn I'm so slow at writing...)
Edit 2 based on comment below
试试这个方法
Try that way