数据库内容未显示,分类事件展示PHP代码故障排查求助
Fixing Category-Based Event Display in PHP
Let's walk through the common issues in your code and fix them step by step:
Key Problems in Your Current Code
- Empty While Loop: Your
while($row2= mysqli_fetch_array($result)) { }doesn't include any code to actually display the data from$row2—it just cycles through the result set without outputting anything. - Unfiltered Query: If
$resultis a generic query without aWHEREclause for the selected category, you're either getting all events (not just the chosen category) or exhausting the result set before your loop runs. - Lack of Error Handling: You don't check if your query executes successfully, making it hard to debug if something goes wrong with the database call.
Corrected Code Example
Here's a revised version that addresses these issues, with security and readability improvements:
<?php // Assume you have an existing mysqli connection stored in $conn // Get the selected category from the POST request $selectedCategory = isset($_POST['category']) ? $_POST['category'] : ''; $result = null; if (!empty($selectedCategory)) { // Use a prepared statement to fetch filtered events (prevents SQL injection) $stmt = $conn->prepare("SELECT * FROM events WHERE category = ?"); $stmt->bind_param("s", $selectedCategory); $stmt->execute(); $result = $stmt->get_result(); // Check for query errors if (!$result) { echo "Error loading events: " . $conn->error; } } else { // Your working else branch: show all events or default content $result = mysqli_query($conn, "SELECT * FROM events"); } // Display the results if ($result && mysqli_num_rows($result) > 0) { while ($row2 = mysqli_fetch_array($result)) { // Replace these with your actual database column names echo "<div class='event-card'>"; echo "<h3>" . htmlspecialchars($row2['event_name']) . "</h3>"; echo "<p>" . htmlspecialchars($row2['event_description']) . "</p>"; echo "<span class='category-tag'>" . htmlspecialchars($row2['category']) . "</span>"; echo "</div>"; } } else { echo "<p>No events found in this category.</p>"; } ?>
Critical Fixes Explained
- Added Output Logic: The while loop now renders HTML for each event using data from
$row2(adjust the column names to match your database schema). - Filtered Prepared Statement: We run a targeted query for the selected category, and use prepared statements to avoid SQL injection vulnerabilities.
- Centralized Result Display: Instead of repeating loop logic for each category, we handle display once after fetching the correct result set.
- Error Checking: We added a check to catch and display database query errors, which helps with debugging.
Quick Form Check
Make sure your category buttons use the correct name attribute so the POST data is captured properly:
<form method="POST" action=""> <button type="submit" name="category" value="Healthcare">Healthcare</button> <button type="submit" name="category" value="Education">Education</button> <button type="submit" name="category" value="Technology">Technology</button> </form>
内容的提问来源于stack exchange,提问作者umang yadav
相关产品推荐
相关产品推荐

