简体   繁体   English

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

[英]How to insert data into an existing html table in php?

I want to insert data which returns from the php code , into an existing html table (name = "userDetails"). 我想将从php代码返回的 数据插入现有的html表中 (名称=“ userDetails”)。

Number of rows in the html table would be different according to the results receiving from the mysql database. html表中的行数会根据从mysql数据库接收到的结果而有所不同。 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/>

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. 您可以在while循环中简单地生成HTML。 Not the best possible strategy, but it certainly gives results. 这不是最好的策略,但肯定可以带来结果。

using same page means it will work. 使用同一页面意味着它会工作。 try this 尝试这个

 while ($row = mysqli_fetch_assoc($result)) 
{
$userDataArray[$index] = $row;
$row = $userDataArray[$index];
echo "<tr><td>".$row['id']."</td>";
echo "<td>".$row['firstname']."</td>"";
echo "<td>".$row['email']."</td></tr>"";
$index++;
}

what are the requirements of these lines in your code actualy? 这些代码实际上对您的代码有什么要求? $userDetailsArray = array(); $ userDetailsArray = array(); $userDataArray[$index] = $row; $ userDataArray [$ index] = $ row; $row = $userDataArray[$index]; $ row = $ userDataArray [$ index]; ??and even what is the requirement of $index variable? 甚至$ index变量有什么要求?

i am adding changes to you program here. 我在这里向您的程序添加更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM