如何根据用户在html界面上选择的内容编写SQL查询? 例如
id | acc_type | bill_charge | Month | Year
_________________________________________________________________
1 | store | 12 | January | 2014
2 | water plant | 11 | January | 2013
3 | store | 51 | February| 2014
4 | pump house | 34 | March | 2014
4 | pump house | 33 | January | 2014
4 | pump house | 5 | January | 2014
4 | pump house | 22 | March | 2013
4 | pump house | 0 | March | 2014
用户将负责查看数据的月份和年份 现在,我能想到的唯一一件事就是重定向到不同的php文件,这个文件每个月都会有QUERY,在那个特定的月份里,从2014-2020这一年起每年都会像这样:
SELECT id , acc_type , bill_charge , month , year
FROM acc_info , acc_details , acc_bill
WHERE acc_info.id = acc_details.id AND Month ="January" AND Year = "2014"
用户将从此输入中选择
<select name="Month" id="Month" width="300px" required/>
<option value="">--Select Month--</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="Year" id="Year" width="300px" required/>
<option value="">--Select Year--</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
有没有更好的方法呢?
答案 0 :(得分:0)
您可以通过表单将值传递给PHP脚本。首先,定义表单:
<form action="script.php" method="POST">
<select name="Month" id="Month" width="300px" required/>
<option value="">--Select Month--</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="Year" id="Year" width="300px" required/>
<option value="">--Select Year--</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</form>
定义表单后,编写一个PHP脚本,用于解析传入的值,连接到数据库并发送请求。例如:
<?php
$Month = $_REQUEST["Month"];
$Year = $_REQUEST["Year"];
// Database example from PHP.net manual
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT id , acc_type , bill_charge , month , year
FROM acc_info , acc_details , acc_bill
WHERE acc_info.id = acc_details.id AND Month ='".$Month."' AND Year = '".$Year."'")) {
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
}
$mysqli->close();
?>
请考虑通过MySQLi(http://php.net/manual/en/book.mysqli.php)或PDO(http://php.net/manual/en/ref.pdo-mysql.php)阅读PHP + MySQL手册。
P.S。请注意,使用$Month = $_REQUEST["Month"]
之类的代码实际上可能不安全并导致SQL注入。有关此内容的更多信息,请参阅此文章:http://php.net/manual/en/security.database.sql-injection.php
<强>更新强>
如果您愿意输出数据,可以这样做:
if ($result = $mysqli->query("SELECT id , acc_type , bill_charge , month , year
FROM acc_info , acc_details , acc_bill
WHERE acc_info.id = acc_details.id AND Month ='".$Month."' AND Year = '".$Year."'")) {
while($output = $result->fetch_assoc) {
echo $output['id']." ".$output['acc_type']." ".$output['bill_charge']." ".$output['month']." ".$output['year']."<br>";
}
/* free result set */
$result->close();
}
答案 1 :(得分:-1)
用户应选择一个日期,例如1月,然后可以将其连接到2014-01-01
,SQL查询可以搜索数据输入的日期范围。 (对于数据库中的数据,您应始终拥有created_at
和updated_at
个时间戳。)