通过日期选择器日历从所选日期获取数据

时间:2015-04-27 16:26:57

标签: php jquery html 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:在下图中,我们可以看到我的数据库表格如何

enter image description here

2 个答案:

答案 0 :(得分:7)

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

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

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

BETWEEN '$firstdate' AND '$lastdate'

对查询使用错误检查会引发语法错误

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

MySQL将datetime读为YYYY-mm-dd

您正在使用dateFormat: 'yy-mm-dd'

另外,请输入您的输入名称属性:

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

并添加

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

如上所述。

答案 1 :(得分:0)

您忘记在表单中添加name="firstdatepicker"name="lastdatepicker",并且需要从$ _POST超全局数组填充变量(我强烈希望 你不要&#39 ; t使用register_globals = on)。

更新了HTML部分:

<script type="text/javascript">
  $(function() {
    $('#firstdatepicker, #lastdatepicker').datepicker({ dateFormat: 'yy-mm-dd' });
  });
</script>

<form method="post" action="graph.php">
<p>First Date: <input type="text" name="firstdatepicker" id="firstdatepicker"></p>

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

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

</form>

在PHP方面:

if (isset($_POST['firstdatepicker'])) {
  $firstDate= $_POST['firstdatepicker'];
  $lastDate= $_POST['lastdatepicker'];

  // forgot the single quotes around the dates ...
  $sql="SELECT typeValue,unit,sensorValue,time FROM sensors WHERE username='$username2' AND time BETWEEN '$firstdate' AND '$lastdate'  ORDER BY time DESC";
}