最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to insert data into an existing html table in php? - Stack Overflow

programmeradmin2浏览0评论

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/>

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/>
Share Improve this question asked Feb 18, 2016 at 8:12 AnushkaMAnushkaM 631 gold badge2 silver badges10 bronze badges 2
  • whats the expected output? – rahul Commented Feb 18, 2016 at 8:16
  • 1 are you trying to fetch data from your database? – Matt Magallo Commented Feb 18, 2016 at 8:16
Add a ment  | 

3 Answers 3

Reset to default 2

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.

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(); $userDataArray[$index] = $row; $row = $userDataArray[$index]; ??and even what is the requirement of $index variable?

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/>
发布评论

评论列表(0)

  1. 暂无评论