Get Data from selected dates by a datepicker calendar

15,698

Firstly, you never define $firstdate $lastdate. You should like this:

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

If it's as you have it in your "time" column, they need to be quoted.

BETWEEN '$firstdate' AND '$lastdate'

using error checking on your query would have thrown a syntax error

I.e.: $result = mysqli_query($con,$sql) or die(mysqli_error($con));

MySQL reads datetime as YYYY-mm-dd

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

Plus, give your inputs name attributes:

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

and adding

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

as stated above.

Share:
15,698
Waaaaat
Author by

Waaaaat

Updated on June 08, 2022

Comments

  • Waaaaat
    Waaaaat almost 2 years

    Hello I have a datepicker calendar and what I want is that we I select the two dates then I want to click on a button it displays me all the data which are 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 as I want. Can you help me please to make it work ???

    Thank you for your time .

    PS: in the image below we can see how my database table look like

    enter image description here