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

提交PHP表单后显示查询结果,php,html,mysql,forms,Php,Html,Mysql,Forms,我目前正在使用php和mysql进行一个学校项目。我创建了一个表单,其中包含三个下拉框,用户可以在其中选择要查找的数据类型。但是,我在提交表单后显示结果时遇到了很多问题。这是我目前的代码: <?php require_once 'connection.php'; ?> <form action="stats.php" method ="post"> <input type="hidden" name="submitted" value="true" />

我目前正在使用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

?>

这两个都不适合我。我已经查看了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>";
  }
}
if(!$result=$conn->query($query)){
模具('Error:'。$conn->Error());
}否则{
而($row=$result->fetch_object()){
回显“$行->年”;
}
}

还将
错误报告(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));

您确定您的查询正在返回行吗?请尝试
var\u dump($result)
查看您从查询中返回的内容。您可能还应该在代码中添加一些错误处理,以确保查询成功运行。您的查询是否实际返回任何数据?在执行查询之前,尝试回显查询以检查是否有错误,然后回显$result->num\u行以检查返回的内容。我尝试过这样做,但它仍然不会显示任何内容。我加入了var_dump($row),只是为了确保我正在检索数据。是的,但我不能让它显示在我的桌子上。
//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));