簡體   English   中英

PHP / MYSQL-選擇選項值未發送?

[英]PHP/MYSQL - Select option value not being sent?

我有以下代碼,應返回特定區域內的所有團隊。 我有一個足球隊數據庫,其中包含“隊”和“州”的表。 組表具有對狀態表的外鍵引用,狀態表具有區域(北,南,東,西)的屬性。

我的主頁上有以下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';
            } 

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

我相信您的where語句上的States.id不是區域ID。 如果要從標簽到選擇語句state.id =?過濾$ region作為參數。 (state.id = $ region)它不會給出任何結果。 因為您正在通過狀態進行過濾。 注意:這只是我對以上代碼的初步了解

我認為您對此沒有任何錯誤...只是您可能過濾了錯誤的屬性^ _ ^

填充區域時發生錯誤,選擇區域名稱前后都有多余的空白。 應該是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM