这就是我想要做的。我让用户输入DATEFROM和DATETO。然后,当用户单击该按钮时,它将从我的表WHERE DATEFROM BETWEEN DATETO执行SELECT *查询。我不知道出了什么问题。请帮忙。
<html>
<head></head>
<body>
<?php
<form method="POST" action="">
DATE FROM: <input type="date" name="datefrom"> TO: <input type="date" name="dateto"> <input type="submit" value="Extract excel file" name="extract"></input>
</form>
$datefrom = "datefrom";
$dateto = "dateto";
if(isset($_POST['extract'])){
mysql_connect("localhost", "root");
mysql_select_db("sample");
$myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "ORDER BY id_biometrics";
$result = mysql_query($myquery);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Employee Number</th>
<th>Date Created</th>
<th>Time Created</th>
<th>Status</th>
</tr>";
while($data = mysql_fetch_row($result))
{
echo("<tr>
<td>$data[0]</td>
<td>$data[1]</td>
<td>$data[2]</td>
<td>$data[3]</td>
<td>$data[4]</td>
</tr>");
}
echo "</table>";
}
?>
</body>
</html>
答案 0 :(得分:1)
为什么这一行没有密码。
mysql_connect("localhost", "root");
希望date_created列为Date或Datetime格式。它不能是int或varchar或任何其他东西。
并在ORDER BY之前添加引号。
$myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY id_biometrics";
答案 1 :(得分:0)
你错过了'
,它应该是
"SELECT *
FROM biometrics
WHERE date_created
BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY id_biometrics";
^
^
答案 2 :(得分:0)
你可以试试吗,你需要在ORDER BY
mysql_connect("localhost", "root", "");
$myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN ('" . $datefrom . "' AND '" . $dateto . "') ORDER BY id_biometrics";
答案 3 :(得分:0)
你在查询中错过了一个引用('),试试这个
if(isset($_POST['extract'])){
mysql_connect("localhost", "root");
mysql_select_db("sample");
$datefrom = $_POST['datefrom'];
$dateto = $_POST['dateto'];
$myquery = "SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY id_biometrics";
$result = mysql_query($myquery);
希望这对你有用