用php将mysql数据加载到html文本框中

yftpprvb  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(347)

我有一个包含数千行代码的表,其中我必须在表的某些“td”中加载一些来自数据库的数据。。。所以我找到了几个上传的例子,但都需要重写整个表。。。所以我问你,有没有更简单的方法来传递这些值​​表中的数据库?
简单地说,用select,我必须能够在“td”中加载包含文本框的数据。无需重写表(考虑到有数千行)。。。
我附上我在上面找到的例子:
1° : 点击按钮将数据从mysql数据库加载到html文本框

omvjsjqw

omvjsjqw1#

要将数据从mysql/mariadb数据库加载到表中,可以使用以下代码:

<!DOCTYPE html>
<?php 
//This is the connection
$mysqli = new mysqli('localhost', 'root', 'DatabasePassword', 'Database Name'); ?>
<html>
<head>
    <title></title>
</head>
<body>
    <div class="content">
        <table class='table table-bordered table-striped'>
        <thead>
            <tr>
            <th>Name</th>
            <th>Address</th>
        </tr>
        </thead>
        <tbody>
            <tr>
                <?php while ($row = $mysqli->fetch_array(MYSQLI_ASSOC)):  ?>
                    <?php foreach ($row as $key => $value): ?>
                    <td alt="<?=$key?>"><?=$value?></td>
                    <?php endforeach; ?>
                <?php endwhile; ?>
            </tr>
        </tbody>
    </table>
    </div>

</body>
</html>
nwnhqdif

nwnhqdif2#

你想填补空缺吗 <table> 数据库数据?
1) 构建sql查询。
2) 执行和检索数据。
3) 将数据注入 <table> 使用 foreach .

<?php
    //Connection to db
    $db = new mysqli('127.0.0.1', 'login', 'pass', 'database');

    //Query
    $sql = "SELECT name, surname FROM example";

    //Execution + error checking
    if(!$result = $db->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }
?>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Surname</th>
        </tr>
    </thead>
    <tbody>
        <?php
            while($row = $result->fetch_assoc()){
                //Displaying results in table rows
                echo "<tr>
                        <td>".$row["name"]."</td>
                        <td>".$row["surname"]."</td>
                </tr>";
            }
        ?>
    </tbody>
</table>

相关问题