Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

php - Select * FROM table WHERE date between two selected dates

I want to display data between two dates. But I want to be able to select those two dates. I don`t know where or how to store those dates in order to be able to add a variable in the SELECT. Please be very specific when you answer. Thank you! This is how my code looks like:

<div class ="row">


<div class ="col-md-3">


    <form action="" method="post" role="form">

        <div class="form-group">

            <label for="startdate">Start date:</label>
            <input class="form-control" type="date" name="startdate" id="startdate" >

        </div>

        <div class="form-group">

            <label for="enddate">Ending date:</label>
            <input class="form-control" type="date" name="enddate" id="enddate" >

        </div>

        <button type="submit" class="btn btn-default">Submit</button>
        <input type="hidden" name="submitted" value="1" />


    </form>

</div>
</div>

<div class="panel panel-default">
<!-- Default panel contents -->


<!-- Table -->
<table class="table">
    <thead>
        <th>Nume</th>
        <th>Prenume</th>
        <th>Serviciu</th>
        <th>Data</th>
        <th>Ora</th>
        <th>Email</th>
        <th>Telefon</th>
        <th>Status</th>
    </thead>



    <?php

        $query = "SELECT * FROM rezervari2 ORDER BY data ASC";
        $result = mysqli_query($dbc, $query);

        while($list = mysqli_fetch_assoc($result)){ 

            $list = data_rezervare($dbc, $list['idrezervare']);
    ?>   

    <tr>
        <td><?php echo $list['nume']; ?></td>
        <td><?php echo $list['prenume']; ?></td>            
        <td><?php echo $list['serviciu']; ?></td>
        <td><?php echo $list['data']; ?></td>
        <td><?php echo $list['ora']; ?></td>
        <td><?php echo $list['email']; ?></td>
        <td><?php echo $list['telefon']; ?></td>
        <td><?php echo $list['status']; ?></td> 
    </tr>

     <?php  } ?>     

</table>



</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the BETWEEN clause:

http://www.tutorialspoint.com/mysql/mysql-between-clause.htm https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between

SELECT dateCol FROM tableName WHERE dateCol BETWEEN '2015-05-01' AND '2015-05-27';

Almost any info about SQL syntax for MySQL can be found on MySQL dev site: https://dev.mysql.com/doc/

To get the values from you form fields you will have to use $_GET["field_name"] or $_POST["field_name"] values acoording to the value of your form's method attribute. The values will be available when you submit the form.

$startdate = $_POST["startdate"];

As an advice, avoid concatenating user values in you queries. Or else, you will open your app for SQL injection.

Take a look at mysqli functions (yes, with an "i" at the end) or, even better, PDO:

http://php.net/manual/book.mysqli.php
http://php.net/manual/book.pdo.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...