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