0

私は現在、php と mysql を使用して学校のプロジェクトに取り組んでいます。ユーザーが探しているデータの種類を選択する 3 つのドロップダウン ボックスがあるフォームを作成しました。ただし、フォームの送信後に結果を表示するのに苦労しています。これが私の現在のコードです:

<?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文にあるとほぼ確信しています。テーブル列のタイトル (Year、Month、$gSelect) が表示されますが、クエリ結果が表示されません。

私が試してみました:

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

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

これらのどちらも私のために働いていません。私はガイダンスのためにphp.netを見てきました。どうしようかまだ迷ってます。誰かが私を助けることができれば、本当に感謝しています。

4

2 に答える 2

1

常に返品を確認してください:

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);も非常に役立ちます。

于 2013-07-08T20:16:19.193 に答える