如何在显示的数据库QUICKFIX上获取所有复选框的ID?

时间:2014-10-27 08:46:17

标签: php html mysql forms checkbox

我在数据库中显示了一个数据表,左侧是复选框。我想找到一种方法将复选框链接到问题编号(ID)。当我点击提交时,我希望所选的ID被回显。我希望有人能够选择他们想要的问题然后显示出来。

  <?php

    $con=mysqli_connect("####","####","#####","###");

   $result = mysqli_query($con,"SELECT * FROM q_and_a ");

 echo "<table border='1'>
  <tr>
<th>Add</th>
<th>#</th>
<th>Question</th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>Answer</th>

</tr>";

while($row = mysqli_fetch_array($result))
 {
 echo "<tr>";

echo '<td><input type="checkbox" name="questions[]" value="$id"></td>';

echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['question'] . "</td>";
echo "<td>" . $row['choiceA'] . "</td>";
echo "<td>" . $row['choiceB'] . "</td>";
echo "<td>" . $row['choiceC'] . "</td>";
echo "<td>" . $row['choiceD'] . "</td>";
echo "<td>" . $row['answer'] . "</td>";
echo "</tr>";
 }
 echo "</table>";

mysqli_close($con);

?>

提交按钮

<form  method="POST" action="makeTest.php">
<input type="submit" name="make" value="Make Test">
</form>

1 个答案:

答案 0 :(得分:0)

对您的代码进行一些编辑然后它就可以了。 1.通过添加不是*的字段来更改查询(以确保性能和显示顺序)

 $result = mysqli_query($con,"SELECT id,question,choiceA,choiceB,choiceC,choiceD,answer FROM q_and_a ");
  1. 然后在阻止打开表单标记(HTML)

    之前
    <?php 
     //above codes will be there as you show before 
    echo '<form  method="POST" action="makeTest.php">';
    while($row = mysqli_fetch_array($result)){
    { $id=$row['id'];  // initialize your id here, so as to pass it in checkbox too
     // then continue with your code
    }
    ?>
    <input type="submit" name="make" value="Make Test">
    </form>
    
  2. 在maketest.php中你可以使用foreach来使用ckeckbox,见下文

        foreach($_POST['questions'] as $questions){
                              //do your job
    
        }