You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP详情页if/else语句编写及逻辑实现求助(含语法修复)

Fixing Your If/Else Logic & Implementing the Random Record Feature

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:

  1. Cleaner PHP/HTML Mix: Uses if (...) : and endif; to switch between PHP and HTML without messy echo statements.
  2. DRY (Don't Repeat Yourself): The selected record display is written once, not duplicated in both if and else blocks.
  3. Random Record Logic:
    • Queries for a random record where Style_ID is 1, 2, or 3 and Length matches the selected record.
    • Checks if a matching random record exists before displaying it (avoids errors if no matches are found).
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:08:12