将下拉框和图片 link 放入 table

Put dropdown box and image link in table

    <html>
        <style>
            table {
                border-collapse: collapse;
            }

            table,th,td{
                border: 1px solid black;
                border-collapse: collapse;
            }
            tr:hover {background-color: #f5f5f5}
            th,tr {
                text-align: center;
            }
            table {
                margin-left: auto;
                margin-right: auto;
                width: 75%;
            }
            th, td {
                padding: 15px;
            }
        </style>
    </html>
    <?php
include 'dbLink.php';

include 'navi.php';


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

$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);

$rowcount = mysqli_num_rows($result);

if ($rowcount > 0) 
{?>

如何在每个 table 行中输入下拉列表和图像 link?另外,当我运行这个页面时,只有第一行的数据反映在table中。其余数据不在table中。有任何修复吗?

    <table>
            <tr>
                <th>Employee ID</th>
                <th>First name</th>
                <th>Last Name</th>
                <th>Username</th>
                <th>Employee Role</th>
                <th>Edit role</th>
                <th>Delete User</th>
            </tr>
        <?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>//dropdwon list with data from employeerole table</td>
                    <td>//image link</td>
                </tr>
                 </table>
    <?php}?>

<?php 
}
}
else
{
    echo "0 results";
}
?>

截图

您混合了 Object Oriented StyleProcedural Style 来编写查询。所以,要讲究面向对象程序化方式编写查询。

我已经为您编写了两种方式的代码。继续。

1) 面向对象风格

dbLink.php

<?
$link = new mysqli("localhost", "my_user", "my_password", "world");
?>

已编辑代码

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

$result = $link->query($sql);
$result1 = $link->query($query);

$rowcount = $result->num_rows;

if ($rowcount > 0) 
{?>
    <table>
            <tr>
                <th>Employee ID</th>
                <th>First name</th>
                <th>Last Name</th>
                <th>Username</th>
                <th>Employee Role</th>
                <th>Edit role</th>
                <th>Delete User</th>
            </tr>
        <?php 
    while ($row = $result->fetch_array(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></td>
                    <td></td>
                </tr>
    <?php}?>
    </table>
<?php } 

else
{
    echo "0 results";
}
?>

2) 程序风格

dbLink.php

<?
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
?>

已编辑代码

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

$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);

$rowcount = mysqli_num_rows($result);

if ($rowcount > 0) 
{?>
    <table>
            <tr>
                <th>Employee ID</th>
                <th>First name</th>
                <th>Last Name</th>
                <th>Username</th>
                <th>Employee Role</th>
                <th>Edit role</th>
                <th>Delete User</th>
            </tr>
        <?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></td>
                    <td></td>
                </tr>
    <?php }?>
    </table>
<?php } 

else
{
    echo "0 results";
}
?>

用户要求:更新代码

<?
$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);

$rowcount = mysqli_num_rows($result);

if ($rowcount > 0) 
{?>
    <table>
        <tr>
            <th>Employee ID</th>
            <th>First name</th>
            <th>Last Name</th>
            <th>Username</th>
            <th>Employee Role</th>
            <th>Edit role</th>
            <th>Delete User</th>
        </tr>
        <?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>//dropdwon list with data from employeerole table</td>
            <td>//image link</td>
        </tr>
            <?php }?>
        </table>
<?php 
} else {
    echo "0 results";
}
?>