使用JQuery表单中的值来选择与表单值匹配的数据库项

时间:2013-11-30 15:02:36

标签: php jquery mysql forms

我正在寻找使用我拥有的表单,并选择某些时间和日期我想使用php从我的数据库中选择与该范围匹配的项目。

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

所以从选择时间&amp; date我希望返回符合所选条件的数据库内容。

我已经从数据库中返回了最后十个项目我只需要帮助扩展我的代码以包含表单标准,因为我希望结果在新页面上。

$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'和'Time'是我的数据库中的行,'Time'也自动包含日期。

我希望这是有道理的,我感谢您的帮助。

编辑:PHP文件

首页:

<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页面

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

1 个答案:

答案 0 :(得分:1)

如果您的时间戳格式为0000-00-00 00:00:00,即2012年10月23日@ 16:30:00 = 2013-10-23 16:30:00,您可以执行以下操作:

//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());

}

我没有测试过这个,但它应该可行。如果没有,请告诉我。

希望这有帮助!

**此外,如果您可以尝试避免将变量的起始字母大写。