我有一个表单,其中包含学生姓名复选框,显示在课程详细信息页面上。如果您提交表格,则允许您将学生分配到班级。
如果您重新访问该页面,它会记住哪些学生已被分配到该课程,并自动检查其相应的框。该表格允许您检查其他学生并提交表格,他们将添加到课程中。
我遇到的麻烦实际上是将学生从课堂上移除。具体来说,我如何跟踪哪些学生已被取消选中"未经检查"在类详细信息页面上,并将它们传递给表单操作页面,然后我可以在其上循环SQL DELETE语句?
以下是我一直在玩的代码:
$class_id = $_GET['class_id'];
$tid = $_POST['teachers'];
$student_count = $_GET['student_count'];
mysqli_query($db,"UPDATE class SET teacher_id = $tid WHERE id=$class_id");
if (isset($_POST['students'])) {
$students = $_POST['students'];
foreach ($students as $student_id) {
if (count($students) < $student_count) {
foreach (/*student that was unchecked from the class.php form*/) {
//delete statement
}
break;
} else {
mysqli_query($db, "UPDATE student_class SET student_id = $student_id WHERE class_id = $class_id");
mysqli_query($db,"INSERT INTO student_class (student_id, class_id) VALUES ($student_id, $class_id)");
}
}
}
答案 0 :(得分:1)
当取消选中时,复选框不会在$_POST
或$_GET
数组中生成一个条目。
但您只需删除数据库中的每个条目,即不再进行检查。 (如果您的表单包含完整的枚举,那么 将取消选中!)
$checkedStudents = array()
//Exemplary: depends on the format of $students. If just an id-array, this can be skipped.
foreach ($students as $student_id) {
$checkedStudents[] = $student_id;
}
$inStr = "(" + implode(",",$checkedStudents) + ")";
$query = "DELETE FROM student_class WHERE class_id = $class_id
AND student_id NOT IN ".$inStr;