【问题标题】:Multiple Choice Quiz - not updating responses多项选择测验 - 不更新响应
【发布时间】:2021-10-21 22:49:54
【问题描述】:

我有一个输出的表单:

  1. Quiz_Assign_ID(将 User_ID 与测验 ID 组合在一起)
  2. 问题 ID
  3. 用于输入响应的复选框(选项 A、选项 B、选项 C)

<div class="Main">
  
  <form method="post" action="LP_Quiz_Student_Quiz_Responses.php">

<?php
$quiz_id = trim($_GET['quiz_id']);
$quiz_assign_id = trim($_GET['quiz_assign_id']);

$i = 1;

$count=1;



$sel_query=("SELECT Quiz.quiz_id, Quiz.quiz_title, Quiz_Questions.quiz_id, Quiz_Questions.quiz_question_id, Quiz_Questions.question, Quiz_Questions.option1, Quiz_Questions.option2, Quiz_Questions.option3, Quiz_Questions.answer FROM Quiz, Quiz_Questions WHERE (Quiz.quiz_id=Quiz_Questions.quiz_id) and (Quiz.quiz_id=?)");

$stmt = $conn->prepare($sel_query); 

$stmt->bind_param("i", $quiz_id);
$stmt->execute();

$result = $stmt->get_result(); // get the mysqli result
 
if($result->num_rows === 0) exit('No Quiz Questions!');
while($row = $result->fetch_assoc()) {  ?>



      <p></p>



    
<p>&nbsp;</p>

  <table>
      <tr>
          <td>
          &nbsp;</td>
          <td>
          <input name="quiz_assign_id" type="hidden" value=" <?php echo $quiz_assign_id; ?>" />&nbsp;<input name="quiz_question_id" type="hidden" value=" <?php echo $row["quiz_question_id"]; ?>" /></td>
          <td>&nbsp;</td>
      </tr>
      <tr>
          <td>
          <h4>Question <?php echo $i++; ?>  </h4>
          </td>
          <td>
          <h4><?php echo $row["question"]; ?>&nbsp;</h4>
          </td>
          <td>&nbsp;</td>
      </tr>
      <tr>
          <td>
          <h4>A</h4>
          </td>
          <td><?php echo $row["option1"]; ?>&nbsp;</td>
          <td>
        
              <input name="Response[<?php echo $quiz_question_id; ?>]" type="checkbox" value="Option A" style="width: 20px" /></td>
      </tr>
      <tr>
          <td>
          <h4>B</h4>
          </td>
          <td><?php echo $row["option2"]; ?>&nbsp;</td>
          <td>
        
              <input name="Response[<?php echo $quiz_question_id; ?>]" type="checkbox" value="Option B" /></td>
      </tr>
      <tr>
          <td>
          <h4>C</h4>
          </td>
          <td><?php echo $row["option3"]; ?>&nbsp;</td>
          <td>
        
              <input name="Response[<?php echo $quiz_question_id; ?>]" type="checkbox" value="Option C" /></td>
      </tr>
 
 
  
   </table>


   <?php
  
  
  }
  
  ?>
 <input name="Submit1" type="submit" value="submit" />
  </form> 

</div>  

提交后,我运行以下脚本,该脚本捕获响应但未获取 question_id 和 Quiz_Assign_ID 并且不更新数据库中的值:

<?php
    


    
    if(!empty($_POST['Response'])) {
    
        foreach ($_POST['Response'] as $value) {



    $quiz_assign_id=trim($_POST['quiz_assign_id']); 
    $quiz_question_id=$_POST['quiz_question_id'];   
    
    
echo 'Answer'; echo $value;
 echo 'Question:';  echo $quiz_question_id;
 echo 'Student ID';  echo $quiz_assign_id; echo 'successfully assigned! <br>';


    
    
    $stmt = $conn -> prepare('UPDATE Quiz_Assign_Student_Question SET response = ? WHERE quiz_assign_id = ? and quiz_question_id =? ');
    
    
    if (
        $stmt &&
        $stmt -> bind_param('sss', $value, $quiz_assign_id, $quiz_question_id) &&
        $stmt -> execute() &&
        $stmt -> affected_rows === 1
    ) {
        echo "<script>
         alert('Responses submitted!'); 
         window.history.go(-2);
         </script>";
        
        
    } else {
        echo"<script>
         window.history.go(-2);
         </script>";

    }
    }
    }
    
    
    
    ?>  

我已经玩了几个小时了,但没有运气。

【问题讨论】:

  • 您能否分享填充隐藏输入的$row["quiz_question_id"] 的代码?
  • @Daantje 傻我,我没有在查询中添加字段 Duh - 谢谢下一个问题是,我只得到最后一个问题的回复,而不是每个问题的回复。三个Q却只记录最后一个的响应?
  • 我在您的表格中只看到一个问题。我看到一个},你忘了分享for 循环代码吗?
  • 每个 $row 输出一个问题和三个选项
  • 使用name="Response[".$row["quiz_assign_id"]."][]",这样你就可以找到答案,否则它们都在一个数组中。

标签: php mysql checkbox


【解决方案1】:

因为您使用的是复选框而不是单选按钮,所以您应该更改输入复选框名称属性,因此它发送一个嵌套数组,其中问题 ID 作为键,答案数组作为值,如下所示: (注意额外的[]另外$quiz_question_id没有定义,你应该使用$row数组。

<input name="Response[<?php echo $row['quiz_question_id']; ?>][]" type="checkbox" value="Option A" style="width: 20px" /></td>

因为现在响应数组是嵌套的,所以问题答案按 question_id 分组。要遍历嵌套的响应数组,请使用以下 for 循环:

foreach ($_POST['Response'] as $question_id => $answered) {
    echo "Question ID:".$question_id."<br />";

    // iterate answers
    foreach ($answered as $answer) {
        echo "Answered: ".$answer."<br />";
    }

    // Or transform to comma separated string
    echo "Answered: ".implode(", ", $answered)."<br />";
}

所以您的 LP_Quiz_Student_Quiz_Responses.php 可能如下所示:

if(!empty($_POST['Response'])) {
    $done = 0;

    foreach ($_POST['Response'] as $quiz_question_id => $values) {
        $quiz_assign_id = trim($_POST['quiz_assign_id']); 
        
        echo 'Answers'; print_r($values); // Values is an array here.
        echo 'Question:';  echo $quiz_question_id;
        echo 'Student ID';  echo $quiz_assign_id; echo 'successfully assigned! <br>';
        
        // Because $values is an array, transform it into a comma separated string to insert them into the database
        // But you could also make a database table with answers and loop over the $values array.
        $value = implode(', ', $values);

        $stmt = $conn->prepare('UPDATE Quiz_Assign_Student_Question SET response = ? WHERE quiz_assign_id = ? and quiz_question_id =? ');
        $stmt->bind_param('sss', $value, $quiz_assign_id, $quiz_question_id); 
        $stmt->execute();

        if ($stmt->affected_rows > 0) {
            $done++;
        }
    }

    echo "<script>";
    if ($done) {
        echo "alert('Responses submitted!');";
    } 
    echo "window.history.go(-2);";
    echo "</script>";
}

要同时适应带有复选框的问题和带有单选按钮的问题,您可以使用相同的响应代码,但在将其转换为字符串之前先检查其是否为数组。

带有单选按钮的问题, 每个问题可能有一个答案:

<input name="Response[<?php echo $row['quiz_question_id']; ?>]" type="radio" value="Option A" style="width: 20px" /></td>

带有复选框的问题, 每个问题可能有多个答案:

<input name="Response[<?php echo $row['quiz_question_id']; ?>][]" type="checkbox" value="Option A" style="width: 20px" /></td>

现在您的 LP_Quiz_Student_Quiz_Responses.php 可能如下所示:

if(!empty($_POST['Response'])) {
    $done = 0;

    foreach ($_POST['Response'] as $quiz_question_id => $values) {
        $quiz_assign_id = trim($_POST['quiz_assign_id']); 
        
        // If it's an array (checkbox), transform to string. 
        // Else it is already a string (radio).
        if (is_array($values)) {        
            $values = implode(', ', $values);
        }

        $stmt = $conn->prepare('UPDATE Quiz_Assign_Student_Question SET response = ? WHERE quiz_assign_id = ? and quiz_question_id =? ');
        $stmt->bind_param('sss', $values, $quiz_assign_id, $quiz_question_id); 
        $stmt->execute();

        if ($stmt->affected_rows > 0) {
            $done++;
        }
    }

    echo "<script>";
    if ($done) {
        echo "alert('".$done." Responses submitted!');";
    } 
    echo "window.history.go(-2);";
    echo "</script>";
}

但是使用上面的代码,您可以适应所有类型的字段类型。

简单来说:

foreach ($_POST['Response'] as $question_id => $answered) {
    echo "Question ID:".$question_id."<br />";

    // Check if array, it's a checkbox or multiple select question
    if (is_array($answered)) {
        // iterate answers
        foreach ($answered as $answer) {
            echo "Answered: ".$answer."<br />";
        }

        // Or transform to comma separated string
        echo "Answered: ".implode(", ", $answered)."<br />";
    } else {
        // radio, hidden, input, select or textarea question
        echo "Answered: ".$answered."<br />";    
    }
}

我希望这会为您指明正确的方向。

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多