dousunle5801 2016-02-18 08:12
浏览 56
已采纳

如何将数据插入到PHP中的现有html表中?

I want to insert data which returns from the php code, into an existing html table (name = "userDetails").

Number of rows in the html table would be different according to the results receiving from the mysql database. How can I do that?

Code is as follows,

<?php
    $connection = mysqli_connect('localhost', 'root', '', 'users');

    $sql = "SELECT id, firstname,email FROM usertable";
    $result = mysqli_query($connection, $sql);

    $userDetailsArray = array();

    if(mysqli_num_rows($result) > 0){

        $index = 0;
        while ($row = mysqli_fetch_assoc($result)) {
            $userDataArray[$index] = $row;
            $row = $userDataArray[$index];

            $userID = $row['id'];
            $firstName = $row['firstname'];
            $email = $row['email'];

            $index++;
        }
    }
?>

<html>
<head></head>

<body>
    <table name = "userDetails">
        <tr>
            <th>User ID</th>
            <th>First Name</th>
            <th>email</th>
        </tr>
    </table>

</body>
<html/>
  • 写回答

3条回答 默认 最新

  • dsgixl5195 2016-02-18 08:22
    关注

    Looks like you're looking for something like this:

    <html>
    <head></head>
    
    <body>
        <table name = "userDetails">
            <tr>
                <th>User ID</th>
                <th>First Name</th>
                <th>email</th>
            </tr>
            <?php
                $connection = mysqli_connect('localhost', 'root', '', 'users');
                $sql = "SELECT id, firstname,email FROM usertable";
                $result = mysqli_query($connection, $sql);
                if(mysqli_num_rows($result) > 0){
    
                    while ($row = mysqli_fetch_assoc($result)) {
                        echo '<tr>';
                        echo '<td>'. $row['id'] .'</td>';
                        echo '<td>'. $row['firstname'] .'</td>';
                        echo '<td>'. $row['email'] .'</td>';
                        echo '</tr>';
                    }
                }
            ?>
        </table>
    
    </body>
    <html/>
    

    You can simply generate HTML in your while loop. Not the best possible strategy, but it certainly gives results.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)