2016-11-23 45 views
0

我有下面的代碼應該返回一個特定區域內的所有團隊。我有一個足球隊的數據庫,其中包含球隊和國家的表格。團隊表具有對州表的外鍵引用,並且州表具有區域(北,南,東,西)的屬性。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'; 
      } 

任何指針在正確的方向將不勝感激。

+0

@RuchishParikh嗯,這似乎不是修復它,我會繼續用它玩 –

+0

你明白在主要頁面的形式填入正確?我們可以看到它的一個樣本嗎? – phobia82

+1

就是這樣,檢查我的答案,你在 phobia82

回答

1

填充區域時出現錯誤選擇區域名稱前後有額外的空白區域。它應該是這樣的:

while($stmt->fetch()){ 
    echo '<option value="' . $region . '">' . $region . '</option>\n'; 
} 
+0

是!非常感謝!我不知道空白將導致此問題 –

+0

當您運行查詢搜索region =「South」時,它不會匹配任何內容,因爲「South」!=「South」它們不是相同的字符串。 – phobia82

+0

謝謝,這是有道理的,我沒有使用PHP很長時間,所以我今天學到了一些新東西 –

0

我相信你的where語句中的states.id不是區域id。如果您將$ region作爲參數從標記過濾到select語句state.id =? (state.id = $ region)它不會給出任何結果。因爲你正在通過狀態進行過濾。 注:這只是我在上面的代碼中第一個認識

我不認爲你對此有什麼錯誤......它只是你可能會過濾錯誤的屬性^ _^

+0

這個區域名稱中有多餘的空格,你無意中複製了舊代碼......我更新了我的select語句搜索的正確代碼片段WHERE states.region =? –

+0

它解決了這個問題嗎?如果沒有...讓我們再試一次^ _^ –

+0

不幸的是,不會再玩它 –