DISABLE ADBLOCK

ADBlock is blocking some content on the site

ADBlock errore

Using values from a JQuery form to select database items that match form values

StackOverflow https://stackoverflow.com/questions/20301733
 Checked

Question

I'm looking to use a form that I have and from choosing certain time and dates I would like to use php to select items from my database that match the range.

<form name="ReadingRange" action="RangeSelect.php" onsubmit="return DataValidation()" method="post" >
  <div data-role="fieldcontain">
  <label for="StartTime">Start Time:</label> <input type="time" name="StartTime" value="00:00:00"/><br>
  <label for="FinishTime">Finish Time:</label> <input type="time" name="FinishTime" value="23:59:00"/><br>
  <label for="StartDate">Start Date:</label> <input type="date" name="StartDate" value="2013-11-30" /><br>
  <label for="FinishDate">Finish Date:</label> <input type="date" name="FinishDate" value="2013-11-30" /><br>
  </div>
<input type="submit" value="Get Data" data-inline="true"/>
</form>

So from the selection of the time & date I would be hoping to return the contents of the database that match the selected criteria.

I've got it returning the last ten items from the database already I just need assistance in extending my code to include the form criteria as I would like the results to be on a new page.

$result = mysql_query("SELECT * FROM impcoursework ORDER BY Time DESC LIMIT 10") or die($result."<br/><br/>".mysql_error());
    while($row = mysql_fetch_array($result))
    {
        echo $row['Temp'] . " " . $row['Time'];
        echo "<br>";
    }

'Temp' and 'Time' are rows in my database and the 'Time' automatically includes the date as well.

I hope this makes sense and I thank you for any assistance.

EDIT: PHP FILES

HOMEPAGE:

<html>
<head>
<title>Home Page</title>

<script type="text/javascript" src="javascript.js"></script>


<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> <!-- For using JQUERY -->

</head>

<body>

<div data-role="header">
    <div data-role="navbar">
        <ul>
            <li><a href="impdex.php" data-icon="home">Home Page</a></li>
            <li><a href="controls.php" data-icon="grid">Controls Page</a></li> <!-- Sets up the header bar -->
        </ul>
    </div>
</div> 

<?php  
    ini_set("display_errors", 1); //Error reporting
    error_reporting(E_ALL);

    //include_once('login.php') ;

    //  connect to the database
    //$conn = mysql_connect($db_hostname, $db_username, $db_password ) ;
    //if (!$conn) die("Unable to connect to MySQL".mysql_error()) ;

    //  select the database
    //mysql_select_db($db_database, $conn) or 
        //die("Unable to select database".mysql_error()) ; 

?>

<div data-role="content">
<center>Information about what happens here. 
<br><br>
<a href="lastresults.php" data-role="button" data-inline="true">Click to show last 10 readings</a> <!-- Button linking to some results -->
<br><br>

<h1>See a more specific range of readings</h1>
<form name="ReadingRange" action="RangeSelect.php" onsubmit="return DataValidation()" method="post" >
  <div data-role="fieldcontain">
  <label for="StartTime">Start Time:</label> <input type="time" name="StartTime" value="00:00:00"/><br>
  <label for="FinishTime">Finish Time:</label> <input type="time" name="FinishTime" value="23:59:00"/><br>
  <label for="StartDate">Start Date:</label> <input type="date" name="StartDate" value="2013-11-30" /><br>
  <label for="FinishDate">Finish Date:</label> <input type="date" name="FinishDate" value="2013-11-30" /><br>
  </div>
<input type="submit" value="View your selected range" data-inline="true"/>
</form>


</center>

PHP PAGE FOR DISPLAYING RANGE

<html>
<head>
<title>Last Results</title>

<script type="text/javascript" src="javascript.js"></script>

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>

<?php  
    ini_set("display_errors", 1); //Error reporting
    error_reporting(E_ALL);

    include_once('login.php') ;

    //  connect to the database
    $conn = mysql_connect($db_hostname, $db_username, $db_password ) ;
    if (!$conn) die("Unable to connect to MySQL".mysql_error()) ;

    //  select the database
    mysql_select_db($db_database, $conn) or 
        die("Unable to select database".mysql_error()) ; 

    ?>
</head>

<body>

<div data-role="header">
    <div data-role="navbar">
        <ul>
            <li><a href="impdex.php" data-icon="home">Home Page</a></li>
            <li><a href="controls.php" data-icon="grid">Controls Page</a></li>
        </ul>
    </div>
</div>

<div data-role="content">
<center>
DOESNT WORK YET<br>
<?php
    if (isset($_POST['StartTime']) && 
    isset($_POST['StartDate']) && 
    isset($_POST['FinishTime']) && 
    isset($_POST['FinishDate']) 
    {

        //Here you could use trim() or mysql_real_escape_string() etc.

        $start = $_POST['StartDate'].' '.$_POST['StartTime'];
        $end = $_POST['FinishDate'].' '.$_POST['FinishTime'];

        $result = mysql_query("SELECT * FROM impcoursework WHERE (Time >= $start) AND (Time <= $end) ORDER BY Time DESC") or die($result."<br/><br/>".mysql_error());

    }

?>

<br><br>

<a href="impdex.php" data-role="button" data-inline="true">Click to return to Home page</a>
</center>
</div>
</center>
</body>
</html>

Solution

If your timestamp is in the format 0000-00-00 00:00:00 i.e. 23 Oct 2012 @ 16:30:00 = 2013-10-23 16:30:00 you could do the following:

//There are some other validation checks you can do as well.
if (isset($_POST['StartTime']) && 
    isset($_POST['StartDate']) && 
    isset($_POST['FinishTime']) && 
    isset($_POST['FinishDate'])) {

    //Here you could use trim() or mysql_real_escape_string() etc.

    $start = $_POST['StartDate'].' '.$_POST['StartTime'];
    $end = $_POST['FinishDate'].' '.$_POST['FinishTime'];

    $result = mysql_query("SELECT * FROM impcoursework 
    WHERE (Time >= '$start')
    AND   (Time <= '$end')
    ORDER BY Time DESC LIMIT 10") or die($result."<br/><br/>".mysql_error());

}

I haven't tested this but it should work. If not, let me know.

Hope this helps!

** Also, if you can try and avoid having the starting letter of your variables capitalised.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow