表格不会填充数据

时间:2013-04-18 16:33:08

标签: php phpmyadmin

我是编程新手,我一直在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);
}

1 个答案:

答案 0 :(得分:0)

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

$products = get_products(); 

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

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

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

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