我有下面的代碼應該返回一個特定區域內的所有團隊。我有一個足球隊的數據庫,其中包含球隊和國家的表格。團隊表具有對州表的外鍵引用,並且州表具有區域(北,南,東,西)的屬性。PHP/MYSQL - 選擇選項值沒有被髮送?
我有我的主網頁下面的HTML/PHP代碼:
<div>
<form method="post" action="regions_filter.php">
<fieldset>
<legend>Filter Teams By Region</legend>
<select name="Region">
<?php
if(!($stmt = $mysqli->prepare("SELECT DISTINCT region FROM states"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($region)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo '<option value=" ' . $region . ' "> ' . $region . '</option>\n';
}
$stmt->close();
?>
</select>
<input type="submit" value="Run Filter"/>
</fieldset>
</form>
</div>
下面是regions_filter.php文件代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<body>
<div>
<table>
<tr>
<td>Teams By Region</td>
</tr>
<tr>
<td>School Name</td>
<td>State Name</td>
<td>State Capital</td>
<td>State Population</td>
<td>Region</td>
</tr>
<?php
if(!($stmt = $mysqli->prepare("SELECT teams.school_name, states.name, states.capital, states.population, states.region FROM teams
INNER JOIN states ON states.id = teams.state_id
WHERE states.region = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("s",$_POST['Region']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
if(!$stmt->bind_result($school, $state, $capital, $population, $region)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli- >connect_error;
}
while($stmt->fetch()){
echo "<tr>\n<td>" . $school . "\n</td>\n<td>" . $state . "\n</td>\n<td>" . $capital . "\n</td>\n</td>"
. $population . "\n</td>\n<td>" . $region . "\n</td>\n</tr>";
}
$stmt->close();
?>
</table>
</div>
</body>
</html>
當我去上我的主要運行過濾器頁面,我被帶到了regions_filter.php頁面,沒有任何結果。唯一顯示的是位於regions_filter.php頁面頂部的預先編碼的html表格。 我相信這個錯誤在下面的代碼片段中。我已經嘗試了與選項值不同的變化,但似乎無法破解它:
while($stmt->fetch()){
echo '<option value=" ' . $region . ' "> ' . $region . '</option>\n';
}
任何指針在正確的方向將不勝感激。
@RuchishParikh嗯,這似乎不是修復它,我會繼續用它玩 –
你明白在主要頁面的形式填入正確?我們可以看到它的一個樣本嗎? – phobia82
就是這樣,檢查我的答案,你在 –
phobia82