【问题标题】:How to check by ID if data has already been output to the website to avoid duplication如何通过ID检查数据是否已经输出到网站以避免重复
【发布时间】:2018-11-01 18:29:10
【问题描述】:

我需要从我的数据库中输出问题,以便用户可以在注册过程中回答这些问题。每个问题有 2 个与之相关的选项,每个问题现在都被输出到站点两次,每个问题下面有一个选项。我需要每个问题出现一次,其相关选项显示在其下方。请在下面查看我的代码。

   <?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 $row['Question'];?></label><input type="hidden" value="<?php echo $row['Question_ID'];?>"/>
   <?php
             $query = "SELECT Choice_ID, Choice FROM Choices WHERE Question_ID = '$questionid';";
             $results = mysqli_query($conn, $query);
             $resultsCheck = mysqli_num_rows($results);
               if($resultsCheck > 0):
                  while($rows = mysqli_fetch_assoc($results)):
        ?>
         <br><select id="choice">
            <option value="<?php echo $rows['Choice_ID']; ?>"><?php echo $rows['Choice']; ?></option>
        </select><br/>
        <?php
        endwhile;
        endif;
        endwhile;
        endif;
        ?>

【问题讨论】:

  • 问题在内循环中打印,因此可能会显示多次。移动打印 q。在内循环之前
  • @EriksKlotins 我已经编辑了上面的代码。显示一个问题,但每个选项现在显示在单独的选择标签中
  • 查看准备好的语句 - 并加入!

标签: php mysql loops while-loop


【解决方案1】:
  1. 您的问题中没有任何&lt;form&gt;,我想您知道自己在做什么并且知道如何处理发布的数据,因为您的&lt;input&gt;&lt;select&gt; 元素没有任何@ 987654324@ 属性。

  2. 正如 Strawberry 所建议的,建议使用准备好的语句。

为了满足您的需要,我对您的代码进行了一些更改,在您的代码中,您必须将&lt;select&gt; 标记从while 循环中取出,而将&lt;option&gt; 标记放在循环中。

所以代码会是这样的:

<?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'];
        echo '<label>'.$row['Question'].'</label><input type="hidden" value="'.$row['Question_ID'].'">';

        $query = "SELECT Choice_ID, Choice FROM Choices WHERE Question_ID = '$questionid';";
        $results = mysqli_query($conn, $query);
        $resultsCheck = mysqli_num_rows($results);
        if($resultsCheck > 0):
            $string = '<br><select id="choice">';
            while($rows = mysqli_fetch_assoc($results)):
                $string .= '<option value="'.$rows['Choice_ID'].'">'.$rows['Choice'].'</option>';
            endwhile;
            $string .= '</select><br>';
            echo $string;
        endif;
    endwhile;
endif;
?>

【讨论】:

    【解决方案2】:

    像这样组织您的代码(为了便于阅读而进行了简化)

    while($question = mysqli_fetch_assoc($result))
    {
       echo "<label for='q'>...; // print question
       echo "<select name='answer'>"  
       while ($answer = mysqli_fetch_assoc($results))
       {
           echo '<option>'.$answer.</option>';
       }
       echo '</select>';
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-08
      • 1970-01-01
      • 2015-11-01
      • 2010-12-29
      • 1970-01-01
      • 2013-09-17
      • 2017-02-05
      • 2012-11-17
      相关资源
      最近更新 更多