gpt4 book ai didi

php - 如何使用复选框将数据添加到表行

转载 作者:行者123 更新时间:2023-11-29 19:05:08 25 4
gpt4 key购买 nike

 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 表”,其他选定的复选框数据不会消失。

如何修复此错误?

我想将数据添加到表中,如下所示考试问题列表

**id   exam_paper_id   question_id**

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

最佳答案

将名称定义为数组

<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;
}
}

关于php - 如何使用复选框将数据添加到表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43567609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com