下拉列表只出现在第 table 行

Dropdown list only appears in the first table row

这是我的查询:

$sql = "SELECT * FROM employee";
$query = "SELECT * FROM employeerole ORDER BY role_id";

$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);
<?php
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    ?>
    <tr>
        <td><?php echo $row["id"]; ?></td>
        <td><?php echo $row["first_name"]; ?></td>
        <td><?php echo $row["last_name"]; ?></td>
        <td><?php echo $row["username"]; ?></td>
        <td><?php echo $row["role"]; ?></td>
        <td>

Here is my code that includes a dropdown box in a table cell however, the data in the dropdown box only appears in the first row of the table (as shown in screenshot below).

Has this got something to do with the loop?

            <form action ="change.php">
                <select id="role" class="" onchange="this.form.submit()">
                    <?php while ($line = mysqli_fetch_array($result1, MYSQL_ASSOC)) { ?>
                    <option value="<?php echo $line['role_id']; ?>"><?php echo $line['role_name']; ?></option>
                    <?php } ?>
                </select>
            </form>
        </td>
        <td><input type="button" onclick="ConfirmDelete()" value="Delete account"</td>
    </tr>
    <?php } ?>

table的屏幕截图:

问题是,结果集 $result1 在第一行本身已用完。您必须调整结果指针以指向结果集的开头,以便您可以再次遍历它。为此使用 mysqli_data_seek() 函数。

..."this.form.submit()"> 行之后添加此行 mysqli_data_seek($result1, 0);

所以你的代码应该是这样的:

// your code

        <form action ="change.php">
            <select id="role" class="" onchange="this.form.submit()">
                <?php 
                    mysqli_data_seek($result1, 0);
                    while ($line = mysqli_fetch_array($result1, MYSQLI_ASSOC)) { 
                ?>
                <option value="<?php echo $line['role_id']; ?>"><?php echo $line['role_name']; ?></option>
                <?php } ?>
            </select>
        </form>
    </td>
    <td><input type="button" onclick="ConfirmDelete()" value="Delete account"</td>
</tr>
<?php } ?>

// your code