在两个选定日期之间选择* FROM表WHERE日期
我想显示两个日期之间的数据.但我希望能够选择这两个日期.我不知道在哪里或如何存储这些日期,以便能够在SELECT中添加变量.回答时请非常具体.谢谢! 这是我的代码的样子:
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>
您可以使用BETWEEN
子句:
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';
关于MySQL的SQL语法的几乎所有信息都可以在MySQL开发站点上找到: https://dev.mysql .com/doc/
Almost any info about SQL syntax for MySQL can be found on MySQL dev site: https://dev.mysql.com/doc/
要从表单字段中获取值,必须根据表单的method属性值使用$_GET["field_name"]
或$_POST["field_name"]
值.提交表单时,这些值将可用.
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"];
作为建议,请避免在查询中串联用户值.否则,您将打开您的应用程序以进行SQL注入.
As an advice, avoid concatenating user values in you queries. Or else, you will open your app for SQL injection.
看看mysqli
函数(是的,结尾处带有"i"),或者甚至更好的是PDO
:
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