0

在 php 中,我想在激活日期内的页面中显示一条消息,这意味着管理员将选择从日期到截止日期的日期,并且在这些选定的日期内,消息将显示在页面中。对于日期选择,我使用了jQuery datepicker。我已经像这样制作了我的数据库

CREATE TABLE IF NOT EXISTS `messagebox` (
  `message_id` int(10) unsigned NOT NULL auto_increment,
  `message` varchar(255) NOT NULL,
  `start_date` date NOT NULL,
  `end_date` date NOT NULL, 
    PRIMARY KEY (`message_id`))
    ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'))

我的管理员表格看起来像这样

<form id="formID"  method="post"  action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">

            <label>Message</label>
            <div class="margin-form"><input class="text-input" type="text"  name="message" id="message" value=""/></div>
            <label>Start Date</label>
            <div class="margin-form"><input class="text-input" type="text"  name="start_date" id="start_date" value=""/> </div>
            <label>End Date</label>
            <div class="margin-form"><input class="text-input" type="text"  name="end_date" id="end_date" value=""/> </div>
            <input type="submit" name="submit_announcement" class="button"  value="Save"/>
            </div>
          </form>

为了插入数据我的代码去

if(isset($_POST['submit_announcement'])) {
            'INSERT INTO `messagebox` (`message_id`,`message`,`start_date`,`end_date`) VALUES ("","'.$message.'","'.$start_date.'","'.$end_date.'") ';
          }
          echo "Settings updated successfully";
          else {
          echo "You have some errors";
          }

在这里我可以看到数据已经插入到数据库中。现在在视图页面中,我正在获取数据。

$sql_query = mysql_query("SELECT * FROM messagebox") or die(mysql_error());
          while($row = mysql_fetch_array($sql_query)){
          $message_details = $row['message'].;
            echo $message_details;
          }

在这里,我收到了所有消息,但我只想显示在该时间格式内设置的消息(从 start_date 到 end_date)。超过结束日期后,该消息将被停用。那么有人可以告诉我如何做到这一点吗?

4

1 回答 1

0

我认为您有一些方法可以在视图页面中获取开始日期和结束日期,假设您必须添加 where 子句来选择位于该日期范围之间的消息。但是您没有插入消息日期的字段。您必须将其添加到表中,然后才能使用BETWEEN运算符。所以查询将是这样的

SELECT * FROM messagebox WHERE dateinserted BETWEEN $start_date AND $end_date;

$start_date并且$end_date应该是具有正确日期格式的字符串。

于 2013-09-28T12:14:50.607 回答