【发布时间】:2019-11-21 14:24:08
【问题描述】:
我已阅读有关此问题的文章和问题,但我仍然觉得很难解决。
我正在尝试实现课程注册。所以我从我的数据库中填充了这个表。
<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>
如果选中了一个复选框,我如何获取数组 (title, code, unit) 的所有值并将它们插入到我的注册表中的相应列上还请注意课程超过 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、title、code 和 unit 到数据库)
【问题讨论】:
-
您的代码易受 SQL 注入攻击。您应该使用准备好的语句。