如何使用复选框

时间:2017-04-23 05:08:56

标签: php mysql

 exam_paper{
    exam_paper_id,
    exam_paper_name
}

question{
    question_id,
    question,
    option1,
    option2,
    option3,
    option4,
    answer
}
exam_question_list{

    id,
    exam_paper,id,
    question_id

}

exampaper.php

 <a href="addquestions_to_exampaper.php?id=<?php echo $row3["exam_paper_id"];?>">
        <button id="addques"><i class="fa fa-plus-circle"></i>&nbsp;Add Questions</button>
        </a>

addquestions_to_exampaper.php

                <?php
                $sql= "SELECT * FROM question WHERE catergory_id=$catergory_id AND level_id=1";
                $result=mysqli_query($dbcon,$sql);

                ?>
<form method="post" action="addquestions_to_exampaper_action.php?exid=<?php echo $exam_paper_id;?>">

<table>

  <caption>Easy Level Questions</caption>

  <thead>
    <tr>
      <th scope="col">Question ID</th>  
      <th scope="col">Question</th>
    </tr>

  </thead>

<tbody>


<?php 
    while($row = mysqli_fetch_array($result)){
?>
    <tr>

      <td data-label="Question Id"><input name="question_id" value="<?php  echo $row['question_id'];?>" readonly type="checkbox" /></td>
      <td data-label="Question" style="text-align:left"><?php echo $row['question']; ?></td>

    </tr> 

<?php
    }
    ?>

  </tbody>
</table>

<input type="submit" value="Add Question" name="submit" id="submit" />

</form>

addquestions_to_exampaper_action.php

<?php
include '../../db/db_connection.php';

    if(isset($_POST['submit'])){
        $exam_paper_id=$_GET['exid'];

        $question_id=$_POST['question_id'];

$sql="INSERT INTO exam_question_list (exam_paper_id,question_id) VALUES ('$exam_paper_id','$question_id')";


         if (mysqli_query($dbcon,$sql)){

            header("Location: exampaper.php");

            exit();
         }else{ 
            $error=mysqli_error($dbcon);
         }



    }   
?>

我有q问题池。 (问题表) 我需要通过选中复选框来添加问题,并且需要在问题文件中添加问题。(exam_question_list表)

当我选择复选框的数量并单击添加按钮时,只有一个复选框数据进入'exam_question_list表',其他选中的复选框数据没有消失。

如何解决此错误?

我想将数据添加到表中,如下所示 exam_question_list

**id   exam_paper_id   question_id**

1         1             12
2         1             3
3         1             45
4         1             5

1 个答案:

答案 0 :(得分:0)

将名称定义为数组

<td data-label="Question Id"><input name="question_id[]" value="<?php  echo $row['question_id'];?>" readonly type="checkbox" /></td>

获取PHP代码中的数组复选框

//now loop them
if(!empty($_POST['question_id'])) {
    foreach($_POST['question_id'] as $check) {
        echo $check;
    }
}