0

我是编程新手,我一直在 localhost 上使用 phpmyadmin。我正在网页上制作一个简单的表格来显示数据。问题是每次我加载页面时它只显示表格而不加载表格。这是我的代码:

<?php
require('../model/database.php');
require('../model/product_db.php');

$products = get_products(); 

if (isset($_POST['action'])) {
    $action = $_POST['action'];
} else if (isset($_GET['action'])) {
    $action = $_GET['action'];
} else {
    $action = 'under_construction';
}



 // Display the product list
    include('view-productList.php');

?>     

这是 view-productList.php:

<?php include '../view/header.php'; ?>


<div id="main">

    <h1>Product List</h1>



   <div id="content">
        <!-- display a table of products -->
        <h2><?php echo $name; ?></h2>
        <table>
            <tr>
                <th>Code</th>
                <th>Name</th>
                <th class="right">Version</th>
                <th>&nbsp;</th>
            </tr>

            <?php foreach ($products as $product) : ?>
            <tr>
                <td><?php echo $product['productCode']; ?></td>
                <td><?php echo $product['name']; ?></td>
                <td class="right"><?php echo $product['version']; ?></td>
                <td><form action="." method="post">
                    <input type="hidden" name="action"
                           value="delete_product" />
                    <input type="hidden" name="product_id"
                           value="<?php echo $product['productID']; ?>" />
                    <input type="hidden" name="category_id"
                           value="<?php echo $product['categoryID']; ?>" />
                    <input type="submit" value="Delete" />
                </form></td>
            </tr>
            <?php endforeach; ?>
        </table>
        <p><a href="?action=show_add_form">Add Product</a></p>
    </div>

</div>
<?php include '../view/footer.php'; ?>

查询页面:

<?php
function get_products() {
    global $db;
    $query = 'SELECT * FROM products
              ORDER BY productID';
    $products = $db->query($query);
    return $products;
}

function get_products_by_category($category_id) {
    global $db;
    $query = "SELECT * FROM products
              WHERE products.categoryID = '$category_id'
              ORDER BY productID";
    $products = $db->query($query);
    return $products;
}

function get_product($product_id) {
    global $db;
    $query = "SELECT * FROM products
              WHERE productID = '$product_id'";
    $product = $db->query($query);
    $product = $product->fetch();
    return $product;
}

function delete_product($product_id) {
    global $db;
    $query = "DELETE FROM products
              WHERE productID = '$product_id'";
    $db->exec($query);
}
4

1 回答 1

0

product_db.php 不应该被注释掉一个 - 假设它是包含“查询页面:”内容的文件。

$products = get_products(); 

应该在包含之后立即出现。

并且您的 for 循环需要获取结果而不仅仅是产品资源:

<?php foreach ($products->fetch() as $product) : ?>

假设 fetch() 与此类资源相关,因为我们看不到您的 db 类。

于 2013-04-18T16:47:59.360 回答