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

时间:2015-02-17 03:43:18

标签: php mysql arrays checkbox submit

我正在为学生进行在线测试/测验并使用PHP和MySQL。目前我有一个表单,我可以将问题和答案添加到数据库中。

但是,通过选择多个复选框,我无法将正确的答案提交到数据库中。我想在名为correct_answer(id,question_id,answer_id)的表中存储所有正确答案,并从名为answers(id,answer_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“答案”表中:

  3. 1 - a

    2 - b

    3 - c

    1. 但是,不是将“id =”1“和”3“添加到”correct_answer“表中(因为”a“和”c“具有这些ID数字),它会添加前两个答案”1“和”2“的ID “..因此,它错误地匹配正确答案并将”a“和”b“设置为正确答案。
    2. 有人能帮帮我吗?我想插入多个正确答案,但前提是它们已被选中..

      在此先感谢,任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

如果选中复选框,则可以使用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 ****/
  }
}