gpt4 book ai didi

php - 使用 PHP 在编辑页面上使用组合框

转载 作者:行者123 更新时间:2023-11-29 13:54:07 24 4
gpt4 key购买 nike

我有两个表,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") ?>

最佳答案

您应该更新代码以使用 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;

关于php - 使用 PHP 在编辑页面上使用组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16155461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com