点击动态分类菜单后无结果,无法实现分类联动查询产品数据库数据
Hey there, let's work through this issue step by step. It sounds like you're trying to load matching products from the product table when a category (pulled from category) is clicked, but no data is showing up. Let's break down the fixes and checks you need to make:
Your category links need to send the c_id value to the backend so it knows which products to fetch. For a basic server-rendered approach, add the ID as a query parameter:
<!-- Example category menu generation --> <?php // Fetch categories first (make sure this part works!) $category_sql = "SELECT c_id, name FROM category"; $category_result = mysqli_query($link, $category_sql); while ($category = mysqli_fetch_assoc($category_result)) { ?> <a href="your_page.php?c_id=<?php echo $category['c_id']; ?>" class="category-item"> <?php echo htmlspecialchars($category['name']); ?> </a> <?php } ?>
If you're using AJAX for dynamic loading, add a data-cid attribute instead:
<a href="#" class="category-item" data-cid="<?php echo $category['c_id']; ?>"> <?php echo htmlspecialchars($category['name']); ?> </a>
Let's build a secure, working query to pull matching products. First, complete and validate your database connection:
<?php // Complete and test your DB connection $link = mysqli_connect("localhost", "root", "", "admin_log"); // Always check for connection errors! if (!$link) { die("Database connection failed: " . mysqli_connect_error()); } // Get the category ID from the request (sanitize it to prevent SQL injection) $c_id = isset($_GET['c_id']) ? (int)$_GET['c_id'] : 0; // Use a prepared statement to safely fetch products $sql = "SELECT * FROM product WHERE c_id = ?"; $stmt = mysqli_prepare($link, $sql); // Bind the integer parameter (the "i" means integer) mysqli_stmt_bind_param($stmt, "i", $c_id); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); // Display the products if (mysqli_num_rows($result) > 0) { while ($product = mysqli_fetch_assoc($result)) { echo "<div class='product-card'>"; echo "<h3>" . htmlspecialchars($product['product_name']) . "</h3>"; echo "<p>" . htmlspecialchars($product['description']) . "</p>"; // Add other product fields here (price, image, etc.) echo "</div>"; } } else { echo "<p>No products found in this category.</p>"; } // Clean up resources mysqli_stmt_close($stmt); mysqli_close($link); ?>
If you still don't see data, check these quick fixes:
- Verify database data: Run
SELECT * FROM product WHERE c_id = X;directly in your database client (like phpMyAdmin) where X is a valid category ID. If no results show up, yourproducttable doesn't have matching records. - Check for typos: Make sure table names (
category,product) and field names (c_id) are spelled correctly (MySQL is case-sensitive on Linux servers!). - Enable error reporting: Add these lines at the top of your PHP file to see hidden errors:
error_reporting(E_ALL); ini_set('display_errors', 1); - AJAX-specific check: If using dynamic loading, confirm your frontend JS is sending the request correctly and rendering the response:
document.querySelectorAll('.category-item').forEach(link => { link.addEventListener('click', function(e) { e.preventDefault(); const cId = this.dataset.cid; fetch(`load_products.php?c_id=${cId}`) .then(res => res.text()) .then(html => { document.getElementById('products-container').innerHTML = html; }) .catch(err => console.log('Error loading products:', err)); }); });
内容的提问来源于stack exchange,提问作者Manali Naik

