0

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>
2
  • So you are wanting to get all the results (Temp) from your db where the Time field is after or equal your StartTime-StartDate and before or equal to your EndTime-EndDate?
    – Rwd
    Nov 30, 2013 at 16:31
  • Yes that is what I'm hoping to achieve! Nov 30, 2013 at 23:39

1 Answer 1

1

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.

10
  • Unfortunately when I use this code and try to select certain dates, it does not load the next page where the query should display. I'm using JQuery and it just prompts "Error Loading Page" Dec 3, 2013 at 19:53
  • As in a 404 or 500 error? Or is a PHP error? Have you tried doing a normal submit without jQuery?
    – Rwd
    Dec 3, 2013 at 20:00
  • Well I know it's able to submit a query as I had it displaying everything in the database. Would it not just be an error in the code? The jQuery result doesn't give any sort of indication of the type of error aside from the fact that there was an error loading the page Dec 3, 2013 at 20:04
  • So, without using jQuery it is or isn't working fine? If you can show the rest of you code for the php file, or check your php error_log to see what error it is outputting, I can try and help.
    – Rwd
    Dec 3, 2013 at 20:07
  • Sorry, it was working fine previously. I'll edit my question to show the php files Dec 3, 2013 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.