我怎样才能在2014-07-01 00:00:00' 2014-07-01 00:00:00' AND' 2014-07-31 23:59:59'&#34 ;;

时间:2014-07-14 17:08:55

标签: php mysql post http-post

我仍然是一个菜鸟程序员。请帮我解决我的问题。我试图编写时间表。我卡在查询部分。我们假设我有一个选择元素,其中包含月份。如果我想选择七月,时间表会显示2014年7月列出的所有活动。如果已经是2015年会怎样?我的查询仍然是静态的(BETWEEN' 2014-07-01 00:00:00' AND' 2014-07-31 23:59:59'";)

<?php

mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("prototype") or die(mysql_error());


if(isset($_POST['submit'])) {
    if($_POST['month'] == 'July') {


        $sql = "SELECT * FROM events WHERE event_schedule BETWEEN '2014-07-01 00:00:00' AND '2014-07-31 23:59:59'";
        $query = mysql_query($sql);


        while ($row = mysql_fetch_array($query)) {
            echo "Title:".$row['event_title']."<br>";
            echo "Description:".$row['event_description']."<br>";
            echo "Schedule:".$row['event_schedule']."<br>";
            echo "Date Created:".$row['date_created']."<br>";
            echo "Posted By:".$row['posted_by']."<br>";
        }
    }

    if($_POST['month'] == 'August') {
        $sql = "SELECT * FROM events WHERE event_schedule BETWEEN '2014-08-01 00:00:00' AND '2014-08-31 23:59:59'";
        $query = mysql_query($sql);


        while ($row = mysql_fetch_array($query)) {
            echo "Title:".$row['event_title']."<br>";
            echo "Description:".$row['event_description']."<br>";
            echo "Schedule:".$row['event_schedule']."<br>";
            echo "Date Created:".$row['date_created']."<br>";
            echo "Posted By:".$row['posted_by'];
        }
    }
}

echo date("F Y");




?>

<html>
    <form method="post">
        <select name="month">
            <option selected="selected">Select a month...</option>
            <option>January</option>
            <option>February</option>
            <option>March</option>
            <option>April</option>
            <option>May</option>
            <option>June</option>
            <option>July</option>
            <option>August</option>
            <option>September</option>
            <option>October</option>
            <option>November</option>
            <option>December</option>
        </select>
        <input type="submit" name="submit" value="Submit">
    </form>
</html>

3 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情

  

$ sql =&#34; SELECT * FROM events WHERE event_schedule LIKE&#39;% - 07 - %&#39;&#34;;

答案 1 :(得分:0)

您可以将PHP变量嵌入查询文本中。所以你可以用date()来拉当前年份:

$queryYear = date('Y');

$sql = "SELECT * FROM events WHERE event_schedule BETWEEN '{$queryYear}-07-01 00:00:00' AND '{$queryYear}-07-31 23:59:59'";

答案 2 :(得分:0)

如前所述,您应该考虑更改代码以使用 MYSQLi PDO 功能。不推荐使用Mysql_ *函数。

至于你的答案,我会推荐两件事。

1 - 在html表单上添加月份值

<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
...

2 - 仅使用一组代码更改PHP,您无需测试每个案例并重复代码。像这样:

if(isset($_POST['submit'])) {


    /*AGAIN THIS IS NOT RECOMMENDED AS IT CAN BE SQLInjected*/
    $sql = "SELECT * 
              FROM events ";
    if ($_POST['month']!=''){
      $sql .= " WHERE extract(YEAR from event_schedule) = extract(YEAR from now())
                  and extract(MONTH from event_schedule) = $_POST['month']";
    }

    $query = mysql_query($sql);

    while ($row = mysql_fetch_array($query)) {
        echo "Title:".$row['event_title']."<br>";
        echo "Description:".$row['event_description']."<br>";
        echo "Schedule:".$row['event_schedule']."<br>";
        echo "Date Created:".$row['date_created']."<br>";
        echo "Posted By:".$row['posted_by']."<br>";
    }
}