phpmyadmin 表只显示一个值,而它应该显示多个值

byqmnocz  于 2022-12-18  发布在  PHP
关注(0)|答案(1)|浏览(145)

我有一个CRUD项目。
为此,我做了一个MySQL数据库使用面向对象的方法,也与HTML,PHP,Bootstrap和CSS.
此数据库中有3个表:客户、订单和产品,对于我的问题,只需要客户和订单。
我遇到的问题是在读取客户(文件)中,当有多个寄存器时,只有第一个出现,我无法找出原因。
如果有人能帮忙,我会很感激:)
代码

<?php

$id_customer = '';

if (isset($_GET["id_customer"]) && !empty(trim($_GET["id_customer"]))) {
    require_once "../connectDB.php";

    $sql = "SELECT * FROM Customers 
            INNER JOIN Orders
            ON Customers.id_customer = Orders.id_customer
            WHERE Customers.id_customer = ?";

    $columns = [
        "ID Cliente" => "id_customer",
        "Nome" => "fn_customer",
        "Apelido" => "ln_customer",
        "Email" => "email_customer",
        "ID Compra" => "id_order",
        "Data" =>"order_dt",
        "Total" => "order_total"
    ];

    if ($stmt = $mysqli->prepare($sql)) {

        // Set parameters
        $param_id = trim($_GET["id_customer"]);

        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("i", $param_id);

        // Attempt to execute the prepared statement
        if ($stmt->execute()) {
            $result = $stmt->get_result(); ?>
                <table class="table table-bordered table-hover">
                    <thead class="thead-dark">
                        <tr>
                            <?php
                            foreach (array_keys($columns) as $heading) { ?>
                                <th scope="col"><?= $heading ?></th>
                            <?php } ?>
                        </tr>
                    </thead>
                    <tbody>
                    <?php

                    $row = $result->fetch_array(MYSQLI_ASSOC); ?>
                    <?php foreach (array_values($columns) as $column) { ?>
                        <td><?= $row[$column] ?></td>
                    <?php } ?>
                </table>
            <?php 
            }
        }
    // Close statement
    $stmt->close();
        
    // Close connection
    $mysqli->close();
}

?>

我试着删除和修改了一些代码行,但并不多,但仍然不起作用。

ghg1uchk

ghg1uchk1#

你没有遍历你的结果。你不想用一个遍历所有结果的foreach来 Package foreach:

while ($row = $result->fetch_array(MYSQLI_ASSOC)) { ?>
     <tr>
        <?php foreach (array_values($columns) as $column) { ?>
            <td><?= $row[$column] ?></td>
         <?php } ?>
     </tr>
     <?php } ?>

相关问题