php – 通过日期选择器日历从选定日期获取数据

您好我有一个datepicker日历,我想要的是我们选择两个日期然后我想点击一个按钮它会显示这些日期之间的所有数据.

这是我的代码……

<?php
include_once 'header.php';


$con = mysqli_connect('localhost','root','smogi','project');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}
$view = ($_GET['view']);
$username2 =$_SESSION['username'];
$sql="SELECT typeValue,unit,sensorValue,time FROM sensors WHERE username='$view' AND time BETWEEN $firstdate AND $lastdate  ORDER BY time DESC";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>


</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr> <b>Type: </b>";
    echo stripslashes($row['typeValue']) . "<br/><b>Unit: </b>";
    echo stripslashes($row['unit']) . "<br/><b>Value: </b>";
    echo stripslashes($row['sensorValue']) . "<br/><b>Date & Time: </b>";
    echo stripslashes($row['time']) . "<br/>";
    echo "--------------------------------------------------------------------------------------------------------------";
    echo "<br/></tr>";
}
echo "</table>";

?>

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    var firstdate = $('#firstdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
    //$( "#firstdatepicker" ).datepicker();
  });
  </script>


    <script>
  $(function() {
    var lastdate = $('#lastdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
    //$( "#lastdatepicker" ).datepicker();
  });
  </script>
</head>
<body>
 <form method="post" action="graph.php">
<p>First Date: <input type="text" id="firstdatepicker"></p>

<p>Last Date: <input type="text" id="lastdatepicker"></p>

<input type="submit" value="Get Data" name="data"/>

</form>


</body>
</html>

但它不能按我的意愿工作.你可以帮助我让它工作吗?

感谢您的时间 .

PS:在下图中我们可以看到我的数据库表是什么样的

解决方法:

首先,你永远不会定义$firstdate $lastdate.你应该这样:

$firstdate = $_POST['firstdatepicker'];
$lastdate = $_POST['lastdatepicker'];

如果它在您的“时间”列中有,则需要引用它们.

BETWEEN '$firstdate' AND '$lastdate'

使用错误检查您的查询会抛出语法错误

> http://php.net/manual/en/mysqli.error.php

即:$result = mysqli_query($con,$sql)或die(mysqli_error($con));

MySQL将日期时间读为YYYY-mm-dd

> https://dev.mysql.com/doc/refman/5.0/en/datetime.html

你正在使用dateFormat:’yy-mm-dd’

另外,给出输入名称属性:

<input type="text" id = "firstdatepicker" name = "firstdatepicker">
<input type="text" id = "lastdatepicker" name = "lastdatepicker">

并添加

$firstdate = $_POST['firstdatepicker'];
$lastdate = $_POST['lastdatepicker'];

如上所述.

上一篇:c# – Converter无法将’system.datetime’类型的值转换为在windows phone 8.1 Datepicker中键入’datetime’


下一篇:Android:日历弹出窗口中的日期选择器