0

我正在尝试将下拉列表嵌入到更新页面,它工作正常,但我在选择选项部分时遇到问题。当显示一个选项时,它也会在列表中复制它。有什么办法可以说如果使用记录不要再与 PHP 一起使用它?

<?php

                        $sql = "SELECT TeamName, TeamID FROM tblTeam";
                        $result = mysql_query($sql);
                        $player_id = $_GET['id'];
                        $current_team = mysql_query("SELECT
                                        tblteam.TeamID,
                                        tblteam.TeamName,
                                        tblplayer.PlayerID,
                                        tblplayer.PlayerTeam,
                                        tblplayer.PlayerName
                                        FROM
                                        tblplayer
                                        INNER JOIN tblteam ON tblplayer.PlayerTeam = tblteam.TeamID
                                        WHERE PlayerID = $player_id LIMIT 1 ");
                        $my_row = mysql_fetch_array($current_team);
                        ?>
                        <select name="TeamName">

                            <option selected value="<?php echo $my_row['TeamID']; ?>"> <?php echo $my_row['TeamName']; ?> </option>
                            <?php
                            while ($row = mysql_fetch_array($result)) {
                                $team_name= $row["TeamName"];
                                $team_id = $row["TeamID"];
                                echo "<option value=\"$team_id\">$team_name</option>";
                            }
                            echo "</select>";
?>
4

1 回答 1

2

如果条件会为您解决问题,这似乎很简单:

while ($row = mysql_fetch_array($result)) {
    $team_name= $row["TeamName"];
    $team_id = $row["TeamID"];
    if($team_id != $my_row['TeamID']){
        echo "<option value=\"$team_id\">$team_name</option>";
    }
}

此外,在您的示例中,您应该始终清理 $_GET / $_POST 参数:

$player_id = intval($_GET['id']);

如果给定格式不是数字,则Intval将返回 0,因此您的 sql 查询从此刻开始是安全的。

于 2013-04-28T00:53:49.307 回答