Get data from selected dates in datepicker calendar

Hello I have a datepicker calendar and I want me to select two dates, then I want to click a button to show me all the data that is in between those dates.

Here is my code ...

<?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>

      

But it doesn't work the way I want. Can you help me please to make it work?

Thank you for your time.

PS: in the image below we can see what the table with the database looks like

enter image description here

+3


source to share


2 answers


First, you never define $firstdate

$lastdate

. You should like the following:

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

      

If so, as you have in the "time" column, they should be listed.

BETWEEN '$firstdate' AND '$lastdate'

      

using error checking in your request would throw a syntax error

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

MySQL reads datetime

likeYYYY-mm-dd



You're using dateFormat: 'yy-mm-dd'

Also, specify the attributes of your inputs:

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

      

and adding

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

      

as mentioned above.

+6


source


you forgot to add name="firstdatepicker"

, and name="lastdatepicker"

in its form, and you need to fill in your variables from the $ _POST superglobal array (and I desperately hope that you are not using register_globals = on).

Updated part of 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 side:

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";
}

      

0


source







All Articles