更新:
我设法纠正了一些事情。但只插入第一行。如果未选择第一行,则不会插入任何内容。
我只希望从填充的表中选中的复选框插入到新表中。
我的表格如下:
<table id="simple-table" class="table table-striped table-condensed responsive">
<thead>
<tr>
<th style="width:5%" class="center">
<label class="pos-rel">
<input type="checkbox" name="checked" class="ace" />
<span class="lbl"></span>
</label>
</th>
<th style="width:32%">Student Name</th>
<th style="width:13%">Adm. No</th>
<th style="width:10%" class="center">CA1 (10%)</th>
<th style="width:10%" class="center">CA2 (10%)</th>
<th style="width:10%" class="center">CA3 (10%)</th>
<th style="width:10%" class="center">Exam (70%)</th>
<th style="width:10%" class="center">Total (100%)</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_POST['loadStudents'])){
$session = clean($_POST["session"]);
$term = clean($_POST["term"]);
$c_taught = clean($_POST["c_taught"]);
$s_taught = clean($_POST["s_taught"]);
$process_limit = clean($_POST["process_limit"]);
$session_phrase = "Session";
$sql = "SELECT `id`, `StudentID`, `StudentName` FROM tbl_students WHERE `StudentClass` = '".$c_taught."' ORDER BY `StudentName` ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$cnt=1;
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$student_name = $row['StudentName'];
$student_id = $row['StudentID'];
?>
<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" name="checked[]" value="<?php echo $row['id'];?>" />
<span class="lbl"></span>
</label>
</td>
<td><?php echo $student_name; ?><input type="hidden" name="student_name[]" value="<?php echo $row['StudentName']; ?>"/></td>
<td><?php echo $student_id; ?><input type="hidden" name="student_id[]" value="<?php echo $row['StudentID']; ?>"/></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="CA1[]" autofocus>'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="CA2[]">'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="CA3[]">'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="Exam[]">'; ?></td>
<td class="center"><?php echo '<input type="text" maxlength="2" size="6" name="Total[]">'; ?></td>
</tr>
<?php $cnt=$cnt+1;}}
else {
$msg = "<span class='red'><h4> No data available for your selection. </h4></span>";
}
}
?>
</tbody>
</table>
我的插图脚本如下:(与表格在同一页面上)
<?php
$enroll_sql = "";
if(isset($_POST['add_assessment'])) {
if(empty($_POST['checked'])){
echo '<script>alertify.alert("No Student is selected.",function(e){if(e){document.location.href = "cass_entry.php";}}).set("labels", {ok:"OK!"}).set("defaultFocus", "ok").set("title", "Assessment")
</script>';
exit();
}
foreach($_POST['checked'] as $id=>$value) {
$session = $_POST['session'];
$term = $_POST['term'];
$c_taught = $_POST['c_taught'];
$s_taught = $_POST['s_taught'];
$student_id = $_POST['student_id'][$id];
$student_name = $_POST['student_name'][$id];
$ca_1 = $_POST['CA1'][$id];
$ca_2 = $_POST['CA2'][$id];
$ca_3 = $_POST['CA3'][$id];
$exam = $_POST['Exam'][$id];
$total = $_POST['Total'][$id];
$enroll_sql .= '
INSERT INTO tbl_subjects_enrollment (`Session`,`Term`,`Student_Class`,`Subject_Name`,`Student_ID`,`Student_Name`,`CA_1`,`CA_2`,`CA_3`,`Exam`,`Total`)
VALUES("'.$session.'", "'.$term.'", "'.$c_taught.'", "'.$s_taught.'", "'.$student_id.'", "'.$student_name.'", "'.$ca_1.'", "'.$ca_2.'", "'.$ca_3.'", "'.$exam.'", "'.$total.'")';
echo $enroll_sql;
if (mysqli_multi_query($conn, $enroll_sql)) {
$success_msg = '<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">
<i class="ace-icon fa fa-times"></i>
</button>
<h4><i class="ace-icon fa fa-check"> Success</i></h4>
<p>Assessment scores were successfully saved.</p></div>';
} else {
echo "Error saving Assessment: " . $conn->error;
}
}
}
?>
答案 0 :(得分:0)
注意,您的第一个复选框未命名为数组且没有值:
<input type="checkbox" name="checked" class="ace" />
这可能会导致一个简单的var $ _POST ['checked'],它不是一个数组(也不是一个字符串,因为它没有值),并且不会在你的foreach中传递。
答案 1 :(得分:0)
看:
foreach($_POST['checked'] as $id=>$value) {
$enroll_sql .= '
INSERT INTO tbl_subjects_enrollment (`Session`,`Term`,`Student_Class`,`Subject_Name`,`Student_ID`,`Student_Name`,`CA_1`,`CA_2`,`CA_3`,`Exam`,`Total`)
VALUES("'.$session.'", "'.$term.'", "'.$c_taught.'", "'.$s_taught.'", "'.$student_id.'", "'.$student_name.'", "'.$ca_1.'", "'.$ca_2.'", "'.$ca_3.'", "'.$exam.'", "'.$total.'")';
echo $enroll_sql;
if (mysqli_multi_query($conn, $enroll_sql)) {
$success_msg = '<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">
<i class="ace-icon fa fa-times"></i>
</button>
<h4><i class="ace-icon fa fa-check"> Success</i></h4>
<p>Assessment scores were successfully saved.</p></div>';
} else {
echo "Error saving Assessment: " . $conn->error;
}
}
每次连接SQL查询=&gt;并在每次添加先前的查询时执行它。 如果你没有忘记你的分号,它会结束插入相同行的太多时间(实际上这就是为什么只有第一个Insert正在工作)。
此处的问题是查询与查询的缺失分号的串联。
为了使您的代码正常工作,您必须不要连接您的查询:
$enroll_sql = '...'; // = not .=
OR
让它连接但是执行查询 OUTSIDE 你的foreach循环(并在SQL查询的末尾添加一个分号:
, "'.$total.'");'; // ; at the end of the query
答案 2 :(得分:0)
我要感谢所有贡献者,特别是@WizardNx。
我最终解决了它(必要性是创造力的母亲):
似乎复选框没有为每一行分配唯一的id
。事实上,如果我选中row_3
并填写所有输入,则会为row_1
插入,但会分配row_3 id
。
这是我做的:
<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace" name="studentSelect[<?php echo $row['id']; ?>]" value="<?php echo $row['StudentID']; ?>" />
<span class="lbl"></span>
</label>
</td>
<td><?php echo $row['StudentName']; ?><input type="hidden" name="student_name[<?php echo $row['id']; ?>]" value="<?php echo $row['StudentName']; ?>"/></td>
<td><?php echo $row['StudentID']; ?></td>
<td class="center"><input type="text" maxlength="2" size="6" name="CA1[<?php echo $row['id']; ?>]" autofocus></td>
<td class="center"><input type="text" maxlength="2" size="6" name="CA2[<?php echo $row['id']; ?>]"></td>
<td class="center"><input type="text" maxlength="2" size="6" name="CA3[<?php echo $row['id']; ?>]"></td>
<td class="center"><input type="text" maxlength="2" size="6" name="Exam[<?php echo $row['id']; ?>]"></td>
<td class="center"><input type="text" maxlength="2" size="6" name="Total[<?php echo $row['id']; ?>]"></td>
</tr>
我在每行的每个输入中添加了id
。