gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:24:44 26 4
gpt4 key购买 nike

我希望使用我拥有的表单,并通过选择某些时间和日期,我想使用 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>

因此,从时间和日期的选择中,我希望返回与所选条件匹配的数据库内容。

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

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

最佳答案

如果您的时间戳格式为 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());

}

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

希望这有帮助!

** 另外,如果您可以尝试避免变量的首字母大写。

关于php - 使用 JQuery 表单中的值来选择与表单值匹配的数据库项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com