0

我有两个表, tblPlayer(PlayerID, PlayerName,PlayerTeam(int) ) 和 tblTeams (TeamID, TeamName)

在一个 PHP 页面上我有一个查找所选播放器的功能,功能如下,

function find_selected_player() {
    global $sel_player;
    if (isset($_GET['id'])) {
        $sel_player = get_player_by_id($_GET['id']);
    } else {
        $sel_player = NULL;
    }
}

我的其他功能如下,

function get_player_by_id($player_id) {
    global $conn;
    $query = "SELECT * ";
    $query .= "FROM tblPlayer ";
    $query .= "WHERE PlayerID =" . $player_id ." ";
    $query .= "LIMIT 1";
    $result_set = mysql_query($query, $conn);
    confirm_query($result_set);

    if ($player = mysql_fetch_array($result_set)) {
        return $player;
    } else {
        return NULL;
    }
}

所以在要编辑的表单上我可以得到所有的值,比如

<input type="text" name="PlayerName" value="<?php echo $sel_player['PlayerName']; ?>" />

但是... :) 当我尝试用相反的方式填充组合框时,我被困在那里。添加一些东西时,这就像一个魅力,但 $sel_player['PlayerTeam'] 只给我 ID :(

<?php include "conn.php" ?>
<?php include "function.php" ?>
<?php find_selected_player() ?>
<h2>Edit: <?php echo $sel_player['PlayerName'] ." ". $sel_player['PlayerLname']; ?></h2>
<form action="player.php" method="post">
<table>
<tr>
   <td>Name: </td>
   <td><input type="text" name="PlayerName" value="<?php echo $sel_player['PlayerName']; ?>" /></td>
   </tr>
   <tr>
   <td>Lastname:</td>
   <td><input type="text" name="PlayerLname" value="<?php echo $sel_player['PlayerLname']; ?>" /></td>
   </tr>
   <tr>
   <td>Team:</td>
   <td>
        <?php
$sql = "SELECT TeamName, TeamID FROM tblTeam";
$result = mysql_query($sql);

echo '<select name="TeamName"><option>';
echo "Choose a team.</option>";
echo '<option selected>' . $sel_player['PlayerTeam']  . '</option>';
while ($row = mysql_fetch_array($result)) {
    $team_name= $row["TeamName"];
    $team_id = $row["TeamID"];
    echo "<option value=\"$team_id\">$team_name</option>";
}
echo "</select>";
        ?>
    </td>
   </tr>
   <tr>
       <td><input type="submit" name="submit" value="Save" /></td>
       <td align="right">&nbsp;</td>
   </tr>
   </table>
</form>
<?php ob_end_flush() ?>
<?php include ("footer.php") ?>
4

3 回答 3

1

您应该更新您的代码以使用 MySQLi_* 或 PDO。我已经让你从这里开始了。试试这个,看看它是如何为你工作的。

function getPlayerByID($pid = '')
{
    global $conn;

    if($pid == '')
        return false;

    $sql = mysql_query("SELECT * FROM tblPlayer WHERE PlayerID = '$pid'", $conn);
    $row = mysql_fetch_array($sql);

    if(mysql_num_rows($sql) == 1)
        return $row;
    else
        return false;
}

$id = isset($_GET['id']) ? $_GET['id'] : '';

$sel_player = getPlayerByID($id);

$sql = mysql_query("SELECT TeamName, TeamID FROM tblTeam");

$select = '<select name=""><option>Choose a Team</option>';

while($row = mysql_fetch_array($sql))
{
    $team_id = $row['TeamID'];
    $team_name = $row['TeamName'];

    $selected = $team_id == $sel_player['PlayerTeam'] ? 'selected' : '';

    $select .= '<option ' . $selected . ' value="' . $team_id . '">' . $team_name . '</option>';
}

$select .= '</select>';

echo $select;
于 2013-04-22T20:29:29.827 回答
0
$team_adi should be $team_name

改变:

echo "<option value=\"$team_id\">$team_adi</option>";

到:

echo "<option value=\"$team_id\">$team_name</option>";
于 2013-04-22T19:51:10.730 回答
0
<?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>";

                            ?>
于 2013-04-28T00:42:11.720 回答