使用 PHP 和 MySQL 提交多个复选框以及文本框输入值?

Submit multiple checkbox together with textbox input value using PHP and MySQL?

我正在为学生制作在线 test/quiz,并使用 PHP 和 MySQL。目前我有一个表格,我可以在其中将问题和答案添加到数据库中。

但是,我无法通过选择多个复选框将正确答案提交到数据库中。我想将所有正确答案存储在名为 correct_answer(id, question_id, answer_id) 的 table 中,并从名为 answers( id,answer_id, 回答).

如何只插入选中的复选框值并跳过未选中的复选框?

我的HTML部分:

<form id="add_question_form" method="post">
  <label>Question: *</label>
  <input id="question" type="text" name="question" class="form_default" placeholder="Type your question here" required>
  <div class="inputs">
    <label>Answer #1: *</label>
    <input type="text" id="answer" name="dynamic[]" class="form_default" placeholder="answer" required/>
    <input type="checkbox" name="selected_item[]" id="checkbox" value="<?php echo $answer_id;?>">
    <label>correct</label>
    <br>
    <label>Answer #2: *</label>
    <input type="text" id="answer" name="dynamic[]" class="form_default" placeholder="answer" required/>
    <input type="checkbox" name="selected_item[]" id="checkbox" value="<?php echo $answer_id;?>" />
    <label>correct</label>
    <br>
  </div>
  <input id="submit" type="submit" name="add_question" value="Submit" class="button">
</form>

PHP代码:

if (isset($_POST['add_question'])) { // Checks if the form has been submitted
 if (isset($_POST['selected_item']) && !empty($_POST['selected_item'])) {
  $counter++; // each time adds 1 after user posts the question

$optionArray = $_POST['dynamic']; // submits multiple answers to the database

  for ($j = 0; $j < count($optionArray); $j++) {
      
 $answer = mysql_real_escape_string($_POST['dynamic'][$j]); // returns multiple answers
     
 $check_answer = "SELECT id, answer FROM answers WHERE answer = '$answer'";
 $check_answer = mysql_query($check_answer); //checks if answer is exist in the database
     
 while ($row = mysql_fetch_assoc($check_answer)) { // fetch a result rows as an associative array
  $answer_id = $row['id']; // answer id from database
 }
     
 if (mysql_num_rows($check_answer) > 0) { 
  $q = "INSERT INTO correct_answer (question_id, answer_id) VALUES ('$counter','$answer_id')";
  $q = mysql_query($q); 
 }
  }
}
}

例如:

  1. 我正在添加问题和三个答案 "a"、"b" 和 "c"。选择 "a" 和 "c" 作为正确答案。

  2. 所有答案已成功添加到MySQL "answers" table:

1 - 一个

2 - b

3 - c

  1. 但是,不是将 id ="1" 和 "3" 添加到 "correct_answer" table 中(因为 "a" 和 "c" 具有这些 id 编号),它添加前两个答案“1”和“2”的 ID。因此,它错误地匹配了正确答案并将 "a" 和 "b" 设置为正确答案。

有人可以帮帮我吗?我想插入多个正确答案,但前提是它们被选中..

在此先致谢,如有任何帮助,我们将不胜感激!

如果复选框被选中,您可以使用 IF statement

for ($j = 0; $j < count($_POST['selected_item']); $j++) {

  if(!empty($_POST['selected_item'][$j])){ /* CHECK IF CHECKBOX IS SELECTED */  

    answer = mysql_real_escape_string($_POST['dynamic'][$j]); // returns multiple answers
    $check_answer = "SELECT id, answer FROM answers WHERE answer = '$answer'";
    $check_answer = mysql_query($check_answer); //checks if answer is exist in the database

    while ($row = mysql_fetch_assoc($check_answer)) { // fetch a result rows as an associative array
      $answer_id = $row['id']; // answer id from database
    }

    if (mysql_num_rows($check_answer) > 0) { 
      $q = "INSERT INTO correct_answer (question_id, answer_id) VALUES ('$counter','$answer_id')";
      $q = mysql_query($q); 
    }
  } /* END OF IF $answer is not empty */

} /* END OF FOR LOOP */

建议:

在您的表单中使用 counter 并在提交前将其放入数组中。示例:

$counter = 0;

 <input type="text" id="answer" name="dynamic[<?php $counter; ?>]" class="form_default" placeholder="answer" value="<?= isset($_POST['dynamic'][0]) ? $_POST['dynamic'][0] : '' ?>" required/>
    <input type="checkbox" name="selected_item[<?php echo $counter; ?>]" id="chechbox" value="<?php echo $answer_id;?>">

然后增加它(假设你也在循环中运行表格)

$counter = $counter + 1;

它不会 运行 循环并且形式是固定的:

您可以手动将数组编号放入文本框和复选框中。

提交后:

并且在提交时,使用PHP的count()函数计算提交的复选框。

$counter = count($_POST["selected_item"]);

然后运行你的循环使用$counter参考我第一个给定的示例代码)。

注:

  • 复选框及其对应的文本框必须具有相同的数组编号,否则答案将与复选框不匹配。

一个更简单的例子:

Answer # 1
<input type="checkbox" name="selected_item[0]"><input type="text" name="dynamic[0]"> <!-- Notice that they are both in 0 array -->

Answer # 2
<input type="checkbox" name="selected_item[1]"><input type="text" name="dynamic[1]"> <!-- Next question array should be incremented from the previous answer -->

提交后,计数 selected_item 然后循环检查选中的复选框。

for($x=0; $x<=count($_POST['selected_item']); $x++){
  if(!empty($_POST['selected_item'][$x])){
    /**** PLACE HERE YOUR INSERT QUERY ****/
  }
}