Select * FROM table WHERE date between two selected dates

I want to display data between two dates. But I want to be able to choose these two dates. I do not know where and how to store these dates in order to be able to add a variable to SELECT. Please be very specific when you reply. Thank! This is what 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> 
-one
php select
May 27 '15 at 17:42
source share
1 answer

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 information on SQL syntax for MySQL can be found at dev dev: https://dev.mysql.com/doc/

To get values ​​from form fields, you will need to use the values $_GET["field_name"] or $_POST["field_name"] depending on the value of your form method attribute. Values ​​will be available when submitting the form.

 $startdate = $_POST["startdate"]; 

As a recommendation, avoid combining custom values ​​in queries. Or you will open the SQL injection application.

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

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

+1
May 27 '15 at 17:49
source share



All Articles