如何根据当前时间从数据库中选择

我正在制作一个学校门户系统,现在我正在制作页面供学生查看作业。我希望他们的作业以红色突出显示,或者如果当前日期超过了截止日期,则根本不显示。我怎样才能做到这一点?我的页面代码


<?php

//including the database connection file

include_once("connection.php");

    $id= $_GET['id'];



//fetching data in descending order (lastest entry first)

$result = mysqli_query($conn, "SELECT * FROM homework where class_id= '$id'"); 


?>


<html>

<head>    

    <title>View IS</title>

</head>


<body>


    <table width='80%' border=0>

        <tr bgcolor='#CCCCCC'>

            <td>Task</td>

            <td>Date Set </td>

            <td>Date Due </td>

        </tr>

        <?php 

        //while($res = mysql_fetch_array($result)) 

        while($res = mysqli_fetch_array($result)) {         

            echo "<tr>";

            echo "<td>".$res['description']."</td>";

            echo "<td>".$res['dateset']."</td>";

            echo "<td>".$res['datedue']."</td>";


        }

        ?>

    </table>

</body>

数据库:

http://img.mukewang.com/62d26ff90001867208050601.jpg

牛魔王的故事
浏览 122回答 3
3回答

慕村225694

而不是 a query,使用 aprepared statement更bind_param安全。然后只需比较日期以检查是否$res['datedue']通过。应该是这样的:<?php&nbsp; &nbsp; include_once("connection.php"); //including the database connection file&nbsp; &nbsp; date_default_timezone_set('America/Los_Angeles');&nbsp; &nbsp; //set the default time zone to your time zone (this is just an example)&nbsp; &nbsp; $result = $conn->prepare("SELECT * FROM homework WHERE class_id=?");&nbsp; &nbsp; $result->bind_param("i", (int)$_GET['id']);&nbsp; &nbsp; $result->execute();&nbsp; &nbsp; $result2 = $result->get_result();?><html><head>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <title>View IS</title></head><body>&nbsp; &nbsp; <table width='80%' border=0>&nbsp; &nbsp; &nbsp; &nbsp; <tr bgcolor='#CCCCCC'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Task</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Date Set </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Date Due </td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($res = $result2->fetch_array(MYSQLI_ASSOC)) {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (date("Y-m-d") > $res['datedue']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr style=\"color: red;\">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['description']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['dateset']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['datedue']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['description']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['dateset']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['datedue']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </table></body>您也可以使用$time = new DateTime;and then$time->format("Y-m-d")代替date("Y-m-d").

杨__羊羊

您应该使用参数化的准备好的语句,而不是手动构建查询。如果日期更大,我已经使用日期函数来比较日期,那么我已经放置了一些样式,而在其他情况下则没有样式。我已经给了你这个想法,现在你可以进行相应的修改。&nbsp; &nbsp; <table width='80%' border=0>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr bgcolor='#CCCCCC'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Task</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Date Set </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>Date Due </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //while($res = mysql_fetch_array($result))&nbsp;&nbsp; &nbsp; $current_date=date("Y-m-d");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($res = mysqli_fetch_array($result)) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($current_date > $res['datedue'] ){?><tr style='color:red;'>;<?php&nbsp; &nbsp; }else {&nbsp; &nbsp; <tr>&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['description']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['dateset']."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>".$res['datedue']."</td>";&nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; </table>

神不在的星期二

不要使用连接的原始 SQL。使用变量绑定来防止 SQL 注入。<?php$stmt = $mysqli->prepare("SELECT * FROM homework where class_id= ?");$stmt->bind_param('i', (int)$_GET['id']); // bind vairable and cast it to integer (it is a good practice when you get data from outside)$stmt->execute();$result = $stmt->get_result();while($res = $result->fetch()){&nbsp; // Your code here&nbsp; var_dump($res);}我会建议使用PDO而不是mysqli. 您可以在此处阅读有关 SQL 注入的更多信息:如何防止 PHP 中的 SQL 注入?不要使用select * (通配符)SELECT description, dateset, datedue, datedue < NOW() AS is_expired FROM homework where class_id= ?检查值is_expired以查看您将标记为红色的结果我还没有尝试过代码,但我想它会按预期工作
打开App,查看更多内容
随时随地看视频慕课网APP