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