我已经阅读了有关此问题的文章和问题,但是我仍然发现很难解决这个问题。
我正在尝试注册课程。因此,我从数据库中填充了该表。
<form method="post" action="courses.php">
<table>
<thead>
<th>Action<th>
<th>Course Title<th>
<th>Course Code<th>
<th>Course Unit<th>
</thead>
<tbody>
<!--Query that fetches the data from the db (included dbcon.php).. the courses are created by admin-->
<tr>
<td><input type="checkbox" name="isChecked[]" value="<?php echo $course_id?>"></td>
<td><input type="hidden" name="ctitle[]" value="<?php echo $title?>"> <?php echo $title?></td>
<td><input type="hidden" name="ccode[]" value="<?php echo $code?>"> <?php echo $code?></td>
<td><input type="hidden" name="cunit[]" value="<?php echo $unit?>"> <?php echo $unit?></td>
</tr>
</tbody>
</table>
<input type="submit" name="enroll">
</form>
如果选中此复选框,那么我如何获取数组(标题,代码,单位)的所有值,并将它们插入到我的注册表的相应列中,请注意课程超过1个? 这是我所做的事情,但是我开始认为我不会继续做下去(想象一下创建数组的另一个foreach吗?初始化一个foreach计数器?)
<?php
if(isset($_POST['enroll'])){
$i=0;
if(!empty ($_POST['isChecked'])){
foreach($_POST['isChecked'] as $course){
$course = $_POST['isChecked'][$i];
$query = "INSERT INTO enrolled (course_id)
VALUES ('$course')";
$insert = $db->query($query);
$i++;
}
}?>
以上将课程ID插入我的数据库非常好。但是我想要的是实际上将所有数据(即ID,标题,代码和单元插入数据库)
答案 0 :(得分:1)
那是因为您的代码是错误的。试试这个:
<tr>
<td><input type="checkbox" name="isChecked['course_id']" value="<?php echo $course_id?>"></td>
<td><input type="hidden" name="isChecked['title']" value="<?php echo $title?>"> <?php echo $title?></td>
<td><input type="hidden" name="isChecked['code']" value="<?php echo $code?>"> <?php echo $code?></td>
<td><input type="hidden" name="isChecked['unit']" value="<?php echo $unit?>"> <?php echo $unit?></td>
</tr>
不要忘记绑定您的值并在执行SQL代码之前检查它们。