如何在php中比较时间戳与Spinner中所选月份的月份

时间:2014-05-29 15:38:49

标签: php mysql

这是我的report.php

<?php

    require("config.inc.php");

    if (!empty($_POST)) {

        $query = "SELECT * FROM user WHERE position = 'user'";

        try {
            $stmt   = $db->prepare($query);
            $result = $stmt->execute();
        }
        catch (PDOException $ex) {

            //die("Failed to run query: " . $ex->getMessage());
            $response["success"] = 0;
            $response["message"] = "Database Error 1. Please Try Again!";
            //die(json_encode($response));
        }

        $row = $stmt->fetchAll();

        //No. of shipment that is requested
        $query = "SELECT p.*,s.* FROM parcel p ,shipmentrequest s 
                    WHERE p.parcelstatus= 'requested'
                    AND p.receiverid = s.receiverid
                    AND MONTH(FROM_UNIXTIME(s.requestTime`)) = 05";

        //Again, we need to update our tokens with the actual data:

        try {
            $stmt   = $db->prepare($query);
            $result = $stmt->execute();
        }
        catch (PDOException $ex) {

            //die("Failed to run query: " . $ex->getMessage());
            $response["success"] = 0;
            $response["message"] = "Database Error 2. Please Try Again!";
            //die(json_encode($response));
        }

        $shipmentrow = $stmt->fetchAll();

        $response["success"] = 1;
        $response["message"] = "Summary Report is displayed";
        //echo json_encode($response);
?> 
        <table border="0px" >
        <tr>
            <td>Number of registered user</td>
            <td><input type="text" name="name" value="<?php echo sizeof($row) ?>" readonly/></td>
        </tr>
        <tr>
            <td>Number of requested shipment</td>
            <td><input type="text" name="name" value="<?php echo sizeof($shipmentrow) ?> "readonly/></td>
        </tr>
        </table>
        <?php
        //for a php webservice you could do a simple redirect and die.
        //header("Location: login.php"); 
        //die("Redirecting to login.php");
    }

    else {
        ?>

    <!DOCTYPE html>
    <html>
        <link rel="stylesheet" type="text/css" href="StylePMSS.css" ></link>

    <head>
        <title>Summary Report</title>

    </head>
    <body >

        <div style="text-align: center">
            <img src="PosMalaysia.png" class="image" height="150" width="550" ></img>
        </div>
        <h1><center>Summary Report</center></h1>
        <center>    
        <form action="report.php" method="post">

        <table border="0px" >
        <tr>
            <td>Month:</td>
            <td><select id='month' name='month'>
                <option value='None'>Select an option</option>
                <option value='01'>January</option>
                <option value='02'>February</option>
                <option value='03'>March</option>
                <option value='04'>April</option>
                <option value='05'>May</option>
                <option value='06'>June</option>
                <option value='07'>July</option>
                <option value='08'>August</option>
                <option value='09'>September</option>
                <option value='10'>October</option>
                <option value='11'>November</option>
                <option value='12'>December</option>
            </select>
            </td>       
        </tr>
        </table>
            <td><input type="submit" value="Check Report" /></td>
        </form>

        </center>

    </body>
    </form>
    </html>
    <?php
    }
    ?>

我想问为什么我的查询$query = "SELECT p.*,s.* FROM parcel p ,shipmentrequest s WHERE p.parcelstatus= 'requested' AND p.receiverid = s.receiverid AND MONTH(FROM_UNIXTIME(s.requestTime)) = 05";

它没有任何MySQL错误,但它并没有返回我想要的结果。 我想要的是,当我从Spinner中选择月份时,它将执行查询并获取根据月份的数据行数。

1 个答案:

答案 0 :(得分:0)

根据您的查询,它将始终从5月(MONTH() = 05)返回项目。如果您希望它返回不同的月份,则必须使用$_POST['month']作为查询的输入。

$query = "SELECT p.*,s.* FROM parcel p ,shipmentrequest s WHERE p.parcelstatus= 'requested' AND p.receiverid = s.receiverid AND MONTH(FROM_UNIXTIME(s.requestTime)) = :month";

$stmt   = $db->prepare($query);
$stmt->bindValue(':month', $_POST['month']);
$result = $stmt->execute();