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

如何通过ID检查数据是否已输出至网站以避免重复展示

问题分析

你现在的核心问题是每个选项都被单独包裹在<select>标签里,每个下拉框仅包含一个选项,再加上问题标签的位置逻辑,导致视觉上呈现出“问题重复出现+单个选项跟随”的效果,完全不符合你想要的「一个问题展示一次,下方列出对应两个选项」的布局。另外,你的代码还存在SQL注入风险,直接拼接变量到SQL语句里是不安全的操作。

修复后的代码(下拉框版本)

我帮你调整了逻辑,把<select>标签移到选项循环的外层,这样每个问题只会生成一个下拉框,里面包含对应的两个选项;同时改用预处理语句规避SQL注入风险,还添加了htmlspecialchars()防止XSS攻击:

<?php
// 查询所有问题
$sql = "SELECT Question_ID, Question FROM Questions;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if($resultCheck > 0):
    while($row = mysqli_fetch_assoc($result)):
        $questionid = (int)$row['Question_ID'];
?>
        <!-- 输出问题标题 -->
        <label><?php echo htmlspecialchars($row['Question']);?></label>
        <input type="hidden" value="<?php echo $questionid;?>"/>
        
        <!-- 单个下拉框包含该问题的所有选项 -->
        <br>
        <select id="choice_<?php echo $questionid;?>">
            <?php
            // 预处理语句查询对应选项,避免SQL注入
            $query = "SELECT Choice_ID, Choice FROM Choices WHERE Question_ID = ?;";
            $stmt = mysqli_prepare($conn, $query);
            mysqli_stmt_bind_param($stmt, "i", $questionid);
            mysqli_stmt_execute($stmt);
            $results = mysqli_stmt_get_result($stmt);
            
            while($rows = mysqli_fetch_assoc($results)):
            ?>
                <option value="<?php echo htmlspecialchars($rows['Choice_ID']); ?>">
                    <?php echo htmlspecialchars($rows['Choice']); ?>
                </option>
            <?php endwhile; ?>
        </select>
        <br/>
<?php
    endwhile;
endif;
?>
额外优化建议(单选按钮版本)

如果你的需求是让用户从两个选项中二选一,使用单选按钮会比下拉框更符合用户交互习惯,布局也更直观,下面是调整后的版本:

<?php
$sql = "SELECT Question_ID, Question FROM Questions;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if($resultCheck > 0):
    while($row = mysqli_fetch_assoc($result)):
        $questionid = (int)$row['Question_ID'];
?>
        <p><?php echo htmlspecialchars($row['Question']);?></p>
        <input type="hidden" value="<?php echo $questionid;?>"/>
        
        <?php
        $query = "SELECT Choice_ID, Choice FROM Choices WHERE Question_ID = ?;";
        $stmt = mysqli_prepare($conn, $query);
        mysqli_stmt_bind_param($stmt, "i", $questionid);
        mysqli_stmt_execute($stmt);
        $results = mysqli_stmt_get_result($stmt);
        
        while($rows = mysqli_fetch_assoc($results)):
        ?>
            <label>
                <input type="radio" name="question_<?php echo $questionid;?>" value="<?php echo htmlspecialchars($rows['Choice_ID']); ?>">
                <?php echo htmlspecialchars($rows['Choice']); ?>
            </label>
            <br>
        <?php endwhile; ?>
        <br/>
<?php
    endwhile;
endif;
?>

这个版本里,每个问题对应的两个选项是互斥的单选按钮,用户只能选择其中一个,完美匹配你“二选一”的业务场景。

内容的提问来源于stack exchange,提问作者Phil135

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:36:35