【问题标题】:Load mySql data in to HTML textBox whit PHP使用 PHP 将 mySql 数据加载到 HTML 文本框
【发布时间】:2019-03-07 10:18:42
【问题描述】:

我有一张包含数千行代码的表格,我必须在表格的某些“td”中加载来自数据库的一些数据...... 徘徊所以我发现了几个上传的例子,但他们都需要重写整个表...... 那请问各位,有没有更简单的方法在表内传递一个数据库的值?

简单地说,我必须能够在包含文本框的“td”中加载数据。 无需重写表(假设有数千行)...

我附上了我在 SO 上找到的示例:

1°:Load data from MySQL database to HTML textboxes on button click

【问题讨论】:

  • 不能真正理解太多,因为你没有任何代码示例,那么除了我们之外你要做什么?

标签: javascript php html mysql web-applications


【解决方案1】:

要将数据从 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>

【讨论】:

    【解决方案2】:

    您想用数据库数据填充&lt;table&gt;?

    1) 构建您的 sql 查询。

    2) 执行和检索数据。

    3) 使用foreach 将数据注入&lt;table&gt;。

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

    【讨论】:

    • 请不要推荐使用(即使作为示例)old、insecure 和 deprecated mysql_* -职能。它们甚至在 PHP 7 中被完全删除。
    • 是的,抱歉,我复制/粘贴了现有项目。
    猜你喜欢
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多