使 SQL 条目仅在当前日期之后出现 - 对于“本地事件”页
我一直在为我正在创建的社区网站制作一个活动页面。它允许他们为新事件创建新的 SQL 条目。
我想要的是仅显示当前日期之前的日期
目前我有:
SELECT * FROM eventsDB ORDER BY eventdate ASC LIMIT 30";
但我想我必须添加类似的内容:
WHERE eventdate > NOW()
对于记录,上面不起作用 ↑
注意:($eventdate = date of事件)
CRONTAB:
<?php
class simpleCMS {
var $host;
var $username;
var $password;
var $table;
public function display_public() {
$q = "SELECT *
FROM eventsDB
WHERE eventdate > UNIX_TIMESTAMP()
ORDER BY eventdate ASC
LIMIT 30";
$r = mysql_query($q);
if ( $r !== false && mysql_num_rows($r) > 0 ) {
while ( $a = mysql_fetch_assoc($r) ) {
$title = stripslashes($a['title']);
$author = stripslashes($a['author']);
$bodytext = stripslashes($a['bodytext']);
$eventdate = stripslashes($a['eventdate']);
$created = stripslashes($a['created']);
$entry_display .= <<<ENTRY_DISPLAY
<div class="post">
<table class="eventstable" cellspacing="0" cellpadding="0">
<tr>
<td><img src="media/icons/icon_calendar.gif"/> <b>$title </b></td>
<td class="right">$eventdate </td>
</tr>
<tr>
<td colspan="2" class="small">$bodytext <i>by $author</i></td>
</tr>
</table>
</div>
ENTRY_DISPLAY;
}
} else {
$entry_display = <<<ENTRY_DISPLAY
<h2> Your brand new Events Page! </h2>
<p>
No entries have been made yet.
Follow my instructions to make a new event!
</p>
ENTRY_DISPLAY;
}
$entry_display .= <<<ADMIN_OPTION
<p class="admin_link">
<a href="{$_SERVER['PHP_SELF']}?admin=97538642"></a>
</p>
ADMIN_OPTION;
return $entry_display;
}
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" method="post">
<label for="title">Title:</label><br />
<input name="title" id="title" type="text" maxlength="150" />
<div class="clear"></div>
<label for="bodytext">Body Text:</label><br />
<textarea name="bodytext" id="bodytext"></textarea>
<div class="clear"></div>
<label for="author">Author:</label><br />
<input name="author" id="author" type="text" maxlength="100" />
<div class="clear"></div>
<label for="eventdate">Date (DD/MM/YY):</label><br />
<input name="eventdate" id="eventdate" type="text" maxlength="100" />
<div class="clear"></div>
<input type="submit" value="Create This Event!" />
</form>
<br />
<a href="../events.php">Back to Events</a>
ADMIN_FORM;
}
public function write($p) {
if ( $_POST['title'] )
$title = mysql_real_escape_string($_POST['title']);
if ( $_POST['bodytext'])
$bodytext = mysql_real_escape_string($_POST['bodytext']);
if ( $_POST['author'])
$author = mysql_real_escape_string($_POST['author']);
if ( $_POST['eventdate'])
$eventdate = mysql_real_escape_string($_POST['eventdate']);
if ( $title && $bodytext && $author ) {
$created = time();
$sql = "INSERT INTO eventsDB VALUES('$title','$bodytext','$created','$author','$eventdate')";
return mysql_query($sql);
} else {
return false;
}
}
public function connect() {
mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
return $this->buildDB();
}
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS eventsDB (
title VARCHAR(150),
bodytext TEXT,
created VARCHAR(100),
author VARCHAR(100),
eventdate VARCHAR(100),
)
MySQL_QUERY;
return mysql_query($sql);
}
}
?>
I've been making an events page for a community website I'm creating. It allows them to create new SQL entries for new events.
What I want is to only display dates ahead of the current date
Currently I have:
SELECT * FROM eventsDB ORDER BY eventdate ASC LIMIT 30";
But I suppose I have to add something like:
WHERE eventdate > NOW()
For the record the above doesnt work ↑
note: ($eventdate = date of event)
CRONTAB:
<?php
class simpleCMS {
var $host;
var $username;
var $password;
var $table;
public function display_public() {
$q = "SELECT *
FROM eventsDB
WHERE eventdate > UNIX_TIMESTAMP()
ORDER BY eventdate ASC
LIMIT 30";
$r = mysql_query($q);
if ( $r !== false && mysql_num_rows($r) > 0 ) {
while ( $a = mysql_fetch_assoc($r) ) {
$title = stripslashes($a['title']);
$author = stripslashes($a['author']);
$bodytext = stripslashes($a['bodytext']);
$eventdate = stripslashes($a['eventdate']);
$created = stripslashes($a['created']);
$entry_display .= <<<ENTRY_DISPLAY
<div class="post">
<table class="eventstable" cellspacing="0" cellpadding="0">
<tr>
<td><img src="media/icons/icon_calendar.gif"/> <b>$title </b></td>
<td class="right">$eventdate </td>
</tr>
<tr>
<td colspan="2" class="small">$bodytext <i>by $author</i></td>
</tr>
</table>
</div>
ENTRY_DISPLAY;
}
} else {
$entry_display = <<<ENTRY_DISPLAY
<h2> Your brand new Events Page! </h2>
<p>
No entries have been made yet.
Follow my instructions to make a new event!
</p>
ENTRY_DISPLAY;
}
$entry_display .= <<<ADMIN_OPTION
<p class="admin_link">
<a href="{$_SERVER['PHP_SELF']}?admin=97538642"></a>
</p>
ADMIN_OPTION;
return $entry_display;
}
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" method="post">
<label for="title">Title:</label><br />
<input name="title" id="title" type="text" maxlength="150" />
<div class="clear"></div>
<label for="bodytext">Body Text:</label><br />
<textarea name="bodytext" id="bodytext"></textarea>
<div class="clear"></div>
<label for="author">Author:</label><br />
<input name="author" id="author" type="text" maxlength="100" />
<div class="clear"></div>
<label for="eventdate">Date (DD/MM/YY):</label><br />
<input name="eventdate" id="eventdate" type="text" maxlength="100" />
<div class="clear"></div>
<input type="submit" value="Create This Event!" />
</form>
<br />
<a href="../events.php">Back to Events</a>
ADMIN_FORM;
}
public function write($p) {
if ( $_POST['title'] )
$title = mysql_real_escape_string($_POST['title']);
if ( $_POST['bodytext'])
$bodytext = mysql_real_escape_string($_POST['bodytext']);
if ( $_POST['author'])
$author = mysql_real_escape_string($_POST['author']);
if ( $_POST['eventdate'])
$eventdate = mysql_real_escape_string($_POST['eventdate']);
if ( $title && $bodytext && $author ) {
$created = time();
$sql = "INSERT INTO eventsDB VALUES('$title','$bodytext','$created','$author','$eventdate')";
return mysql_query($sql);
} else {
return false;
}
}
public function connect() {
mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
return $this->buildDB();
}
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS eventsDB (
title VARCHAR(150),
bodytext TEXT,
created VARCHAR(100),
author VARCHAR(100),
eventdate VARCHAR(100),
)
MySQL_QUERY;
return mysql_query($sql);
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我们上面的小讨论,似乎最简单的事情就是使您的
eventdate
字段成为 INT 数据类型。这样,当您将用户输入作为字符串(例如“15/03/2011”)时,您可以通过 PHP 函数strtotime()
运行该输入并从中获取 UNIX 时间戳。请注意,
strtotime()
返回 INT(或布尔值 FALSE),因此我们不会为您设置上述 SQL 注入攻击。要查询数据库,您可以执行以下操作:要回答此答案评论中的问题:
将替换为
请注意,对于生产系统,我真的不建议放置管理代码在与显示代码相同的页面中,您应该使用 isset() 函数检查是否已设置 $_POST 数组变量(以免导致各种错误) Web 服务器日志中的警告或通知)。
要显示日期,您可以使用 PHP
date()
函数,具体来说:其中
$eventdate
是您从数据库检索的 UNIX 时间戳。Based on our little discussion above, it seems like the easiest thing to do is make your
eventdate
field an INT data type. That way, when you take the user input as a string ("15/03/2011" for example), you can run that input through the PHP functionstrtotime()
and get a UNIX timestamp from that.Note that
strtotime()
returns an INT (or boolean FALSE) so we're not setting you up for a SQL injection attack above. To query the database, you could then do this:To answer your question in the comments to this answer:
Would be replaced with
Note that for a production system, I really wouldn't recommend putting the admin code in the same page as the display code, and you should use the
isset()
function to check whether a$_POST
array variable has been set (lest you cause all sorts of warnings or notices in your web server logs).To display the date, you'd use the PHP
date()
function, specifically:Where
$eventdate
is the UNIX timestamp you retrieved from the database.数据库用于使用或存储数据。
PHP(或您选择使用的任何内容)用于与用户交互。
如果要存储日期,则应该使用日期数据类型。
您想要如何显示日期取决于您的 PHP 代码。
可能您希望根据访问者以不同的格式显示日期,或者可能以 ISO 格式显示日期,以便全世界都可以读取它:YYYY-MM-DD
因此,您的查询几乎是正确的:
请注意,使用“SELECT *”不建议用于生产代码。仅选择您需要的列 - 这可能意味着更好地使用索引并减少数据库和应用程序服务器之间传输的数据。
The database is used or storing data.
PHP (or whatever you chose to use) is used for interacting with the user.
If you want to store a date, you should use a date datatype.
How you want to display the date, is up to your PHP code.
Probably you want to show the date in different formats depending on the visitor, or possible in ISO format so all of the world can read it: YYYY-MM-DD
So, your query is pretty much correct:
Note that using 'SELECT *' is not recommended for production code. Only select the columns you need - it can mean better use of indices and less data transferred between your database and application server.
您可以将
eventdate
字段存储为 UNIX 时间戳,然后在查询中轻松比较它。您可以使用 php
date
函数对其进行格式化:这是手册:PHP 日期函数
You can store
eventdate
field as UNIX timestamp and then compare it easily in your queries.and you can format it with php
date
function :here is the manual : PHP Date Function