繁体   English   中英

提交PHP表单后显示查询结果

[英]Displaying query results after submitting a PHP form

我目前正在使用php和mysql进行学校项目。 我创建了一个带有三个下拉框的表单,用户可以在其中选择要查找的数据类型。 但是,提交表单后,我很难显示结果。 这是我当前的代码:

<?php
require_once 'connection.php';
?>


<form action="stats.php" method ="post">
<input type="hidden" name="submitted" value="true" />

<fieldset>
<legend>
Specify Date, Month, and County
</legend>
<p>
<label for="year">
Please select a year
</label>

<select name= 'year'>
<?php
$query = "select distinct year from unemployed";

$result = $conn->query($query);
while($row = $result->fetch_object()) {
  echo "<option value='".$row->year."'>".$row->year."</option>";
 }
?>
</select>
</p>

<p>
<label for="month">
Please select a month
<label>

<select name= 'month'>
<?php
$query = "select distinct month from unemployed";

$result = $conn->query($query);
while($row = $result->fetch_object()) {
  echo "<option value='".$row->month."'>".$row->month."</option>";
 }
?>
</select>
</p>

<p>
<label for="location">
Please specify a location
</label>

<select name='select'>
<?php
$query = "select * from unemployed";

$result = $conn->query($query);

while ($finfo = $result->fetch_field()) {
  echo "<option value='".$finfo->name."'>".$finfo->name."</option>";
 }

?>
</select>
</p>


<input type ="submit" />

</fieldset>
</form>

<?php

if (isset($_POST['submitted'])) {

include('connection.php');

$gYear = $_POST["year"];
$gMonth = $_POST["month"];
$gSelect = $_POST["select"];

$query = "select $gSelect from unemployed where year='$gYear' and month='$gMonth'";

$result = $conn->query($query) or die('error getting data');


echo"<table>";
echo "<tr><th>Year</th><th>Time</th><th>$gSelect</th></tr>";

while ($row = $result->fetch_object()){

echo "<tr><td>";
echo $row['Year'];
echo "</td><td>";
echo $row['Month'];
echo "</td><td>";
echo $row['$gSelect'];
echo "</td></tr>";

}




echo "</table";

} // end of main if statement

?>

我几乎可以确定我的问题出在我的while陈述中。 我可以显示表列的标题(年,月,$ gSelect),但是查询结果却无法显示。

我努力了:

while ($row = $result->fetch_object())

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

这些都不适合我。 我看了php.net以获得指导。 我仍然对做什么感到困惑。 如果有人可以帮助我,我将非常感激。

始终检查您的退货:

if( ! $result = $conn->query($query) ) {
  die('Error: ' . $conn->error());
} else {
  while($row = $result->fetch_object()) {
    echo "<option value='".$row->year."'>".$row->year."</option>";
  }
}

还放了error_reporting(E_ALL); 在您开发脚本的顶部时,它也会有很大帮助。

您应该真正考虑将变量作为参数传递给查询,而不是将它们作为变量直接注入到查询中。 这可能导致sql注入攻击。

另外,这是一个如何使用PDO和mysql编写查询的快速示例:

//Simple Query
$dbh = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
//Useful during development.
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//mysql can have prepares depending on the version: http://stackoverflow.com/questions/10113562/pdo-mysql-use-pdoattr-emulate-prepares-or-not
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sth = $dbh->query("SELECT * FROM table");
var_dump($sth->fetchAll(PDO::FETCH_ASSOC));
//Now with pass params and a prepared statement:
$query = "SELECT * FROM table WHERE someCol = ?";
$sth = $dbh->prepare($query);
$sth->bindValue(1,"SomeValue");
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM