-1

我想问一些关于我的代码的问题。在 MySQL 数据库中,有两个表,即表'Team'和表'Borrower'。

下面是表“团队”的图像

在此处输入图像描述

下面是表“借款人”的图像

在此处输入图像描述

在 PHP 页面,用户可以编辑任何用户的详细信息,包括他们的团队。从上表中可以看出,Namron team_id 为 3,即团队“燃烧器”。但是在页面上,我想如何在团队选择选项中显示,将列出所有在“团队”表中具有但选择为“燃烧器”的团队,因为这是 Namron 的当前团队。

以下是我当前的代码。

借款人详细信息.php

<?php

    $badgeid = $_POST['badgeid'];

    $sql = "SELECT * 
            FROM ets_borrower 
                INNER JOIN ets_team ON ets_borrower.team_id = ets_team.team_id 
            WHERE ets_borrower.status_id = 1 
              AND ets_borrower.badgeid = :badgeid 
            ORDER BY ets_borrower.fullname ASC";
    
    $query = $conn->prepare($sql);
    $query->execute(array(':badgeid' => $badgeid));
    while($row = $query->fetch(PDO::FETCH_ASSOC)){
        $badgeid = $row["badgeid"];
        $fullname = $row["fullname"];
        $team_id = $row["team_id"];
        $team_name = $row["team_name"];
    }

    $sql2 = "SELECT * FROM ets_team WHERE status = 1";
    $query2 = $conn->prepare($sql2);
    $query2->execute();

    while($row2 = $query2->fetch(PDO::FETCH_ASSOC)){
    
        $team_id2 = $row2["team_id"];
        $team_name2 = $row2["team_name"];

?>

    <div class="form-group row">
        <div class="col-xs-3">
            <label for="example-search-input">Badge ID</label>
        </div>
        <div class="col-xs-9">
            <input class="form-control" type="text" name="badgeid" value="<?php echo $badgeid; ?>" readonly>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-xs-3">
            <label for="example-search-input">Name</label>
        </div>
        <div class="col-xs-9">
            <input class="form-control" type="text" name="fullname" value="<?php echo strtoupper($fullname); ?>" readonly>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-xs-3">
            <label for="example-search-input">Team</label>
        </div>
        <div class="col-xs-9">
            <select class="form-control" id="exampleFormControlSelect1" name="team_id">
                <option value="<?php echo $team_id;?>" <?php echo $team_id2 == $team_id? 'selected': '';?> ><?php echo $team_name2;?></option>
            </select>
        </div>
    </div>
4

2 回答 2

1

将您的 while 循环移动到选择标记下方。

<select class="form-control" id="exampleFormControlSelect1" name="team_id">
<?php
while($row2 = $query2->fetch(PDO::FETCH_ASSOC)){
        $team_id2 = $row2["team_id"];
        $team_name2 = $row2["team_name"];
?>
<option value="<?php echo $team_id;?>" <?php echo $team_id2 == $team_id? 'selected': '';?> ><?php echo $team_name2;?></option>
<?php }
?>
</select>
于 2020-07-01T07:40:29.777 回答
0

有关详细信息,请参阅代码中的注释。

基本上,您必须将应该加载下拉列表的 while 循环移动到仅要加载选项列表的位置

$badgeid = $_POST['badgeid'];

$sql = "SELECT * 
        FROM ets_borrower 
            INNER JOIN ets_team ON ets_borrower.team_id = ets_team.team_id 
        WHERE ets_borrower.status_id = 1 
          AND ets_borrower.badgeid = :badgeid 
        ORDER BY ets_borrower.fullname ASC";

$query = $conn->prepare($sql);
$query->execute(array(':badgeid' => $badgeid));

// while loop not require, only one row can be returned
$row = $query->fetch(PDO::FETCH_ASSOC);
$badgeid = $row["badgeid"];
$fullname = $row["fullname"];
$team_id = $row["team_id"];
$team_name = $row["team_name"];

$sql2 = "SELECT * FROM ets_team WHERE status = 1";
$query2 = $conn->prepare($sql2);
$query2->execute();
// while loop moved down to load just the dropdown
?>

<div class="form-group row">
    <div class="col-xs-3">
        <label for="example-search-input">Badge ID</label>
    </div>
    <div class="col-xs-9">
        <input class="form-control" type="text" name="badgeid" value="<?php echo $badgeid; ?>" readonly>
    </div>
</div>
<div class="form-group row">
    <div class="col-xs-3">
        <label for="example-search-input">Name</label>
    </div>
    <div class="col-xs-9">
        <input class="form-control" type="text" name="fullname" value="<?php echo strtoupper($fullname); ?>" readonly>
    </div>
</div>
<div class="form-group row">
    <div class="col-xs-3">
        <label for="example-search-input">Team</label>
    </div>
    <div class="col-xs-9">
        <select class="form-control" id="exampleFormControlSelect1" name="team_id">
<?php
while($row2 = $query2->fetch(PDO::FETCH_ASSOC)) :

    $sel = $team_id2 == $team_id? 'selected' : '';
    echo "<option value='$team_id' $sel>$row2[team_name]</option>";
endwhile;
?>
        </select>
    </div>
</div>
于 2020-07-01T07:41:05.507 回答