获取某人选择的2个日期之间的记录 php mysql

发布于 2024-10-18 07:33:03 字数 476 浏览 3 评论 0原文

我正在尝试获取两个日期之间的记录。日期以“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就是爱搞怪 2024-10-25 07:33:03

您可以使用 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

流殇 2024-10-25 07:33:03
$start = mysql_real_escape_string(trim($_POST["start"]));
$end = mysql_real_escape_string(trim($_POST["end"]));

$sql_query = "SELECT * FROM actionlist WHERE date BETWEEN '".$start." 00:00:00' AND '".$end." 23:59:59' ORDER BY id";
$result = mysql_query($sql_query) or die(mysql_error());

编辑:trim()以防您的输入表单中有一些不需要的空格;然后进行一些卫生处理,并使用 BETWEEN ,正如每个人都在我之前建议的那样(该死的我写得太慢了......)

根据下面的评论编辑 2

$start = mysql_real_escape_string(trim($_POST["start"]));
$end = mysql_real_escape_string(trim($_POST["end"]));

$sql_query = "SELECT * FROM actionlist WHERE date BETWEEN '".$start." 00:00:00' AND '".$end." 23:59:59' ORDER BY id";
$result = mysql_query($sql_query) or die(mysql_error());

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

淡淡的优雅 2024-10-25 07:33:03

试试这个方法

SELECT * FROM actionlist WHERE date BETWEEN 'start' AND 'end'

Try that way

SELECT * FROM actionlist WHERE date BETWEEN 'start' AND 'end'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文