PHP详情页if/else语句编写及逻辑实现求助(含语法修复)
Let's break down the issues with your original code and build the correct implementation step by step:
First: Fixing the Syntax Error
The main problem with your attempt was mixing PHP and HTML incorrectly inside the else block. You can't drop raw HTML directly into a PHP curly brace block without either echoing it or switching out of PHP mode. Using PHP's alternative control structure syntax (if (...) : / endif;) makes this much cleaner and avoids syntax errors.
Full Working Implementation
Here's the complete code that meets your requirements, with comments explaining each part:
<?php require_once('dbconnection.php'); mysqli_select_db($conn, $dbname); // Get the selected record ID from the URL $recordID = $_GET['recordID']; // Fetch the selected record details $query_Master_details = " SELECT * FROM Master_List WHERE Master_Id = $recordID "; $Master_details = mysqli_query($conn, $query_Master_details) or die(mysqli_error()); $row_Master_details = mysqli_fetch_assoc($Master_details); // Extract values we need for the condition and random record query $selectedStyleId = $row_Master_details['Style_ID']; $selectedLength = $row_Master_details['Length']; ?> <div class="container2"> <?php if ($selectedStyleId > 3): ?> <?php // Query for a random matching record (Style 1/2/3, same length as selected) $query_random = "SELECT * FROM Master_List WHERE Style_ID IN (1,2,3) AND Length = $selectedLength ORDER BY RAND() LIMIT 1"; $result_random = mysqli_query($conn, $query_random) or die(mysqli_error()); $row_random = mysqli_fetch_assoc($result_random); ?> <?php if ($row_random): ?> <!-- Display the random record using your existing layout --> <div class="category"><h2><?php echo $row_random['Name']; ?></h2></div> <p><?php echo $row_random['Name']; ?></p> <p><img src="img/<?php echo $row_random['Img']; ?>" /></p> <p><?php echo $row_random['Code']; ?></p> <p><?php echo $row_random['Length']; ?> Characters</p> <!-- Add spacing between random and selected record --> <br><br> <?php endif; ?> <?php mysqli_free_result($result_random); ?> <?php endif; ?> <!-- Display the selected record (shared for both conditions) --> <div class="category"><h2><?php echo $row_Master_details['Name']; ?></h2></div> <p><?php echo $row_Master_details['Name']; ?></p> <p><img src="img/<?php echo $row_Master_details['Img']; ?>" /></p> <p><?php echo $row_Master_details['Code']; ?></p> <p><?php echo $row_Master_details['Length']; ?> Characters</p> <?php mysqli_free_result($Master_details); ?> </div><!-- end .container2 -->
Key Improvements:
- Cleaner PHP/HTML Mix: Uses
if (...) :andendif;to switch between PHP and HTML without messy echo statements. - DRY (Don't Repeat Yourself): The selected record display is written once, not duplicated in both
ifandelseblocks. - Random Record Logic:
- Queries for a random record where
Style_IDis 1, 2, or 3 andLengthmatches the selected record. - Checks if a matching random record exists before displaying it (avoids errors if no matches are found).
- Queries for a random record where
- Proper Resource Handling: Frees both result sets to avoid memory leaks.
Security Note (Bonus):
Your original code uses $_GET['recordID'] directly in SQL queries, which is vulnerable to SQL injection. For a more secure version, use prepared statements:
// Replace the selected record query with this: $query_Master_details = " SELECT * FROM Master_List WHERE Master_Id = ? "; $stmt = mysqli_prepare($conn, $query_Master_details); mysqli_stmt_bind_param($stmt, "i", $recordID); mysqli_stmt_execute($stmt); $Master_details = mysqli_stmt_get_result($stmt);
Do the same for the random record query to fully protect against injection attacks.
内容的提问来源于stack exchange,提问作者Hevnlymom

