UPDATE:
I have managed to correct some things. But only the first row is inserted. If the first row is not selected, nothing gets inserted.
I want only selected checkboxes from the populated table inserted into the a new table.
My table is as follows:
<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>
My inset script is as follows: (On the same page with the form)
<?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;
}
}
}
?>
Watch, your first checkbox isn't named as an array and has no value :
<input type="checkbox" name="checked" class="ace" />
It may result in a simple var $_POST['checked'] which isn't an array (nor a string since it hasn't value) and won't be passed in your foreach.
Look :
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;
}
}
You concatenate your SQL query each time => and execute it each time adding the previous queries. If you didn't forget your semi-colon it would have ended inserting too many time the same line (and actually that's why only the first Insert is working).
The problem here is the concatenation of the query AND the missing semi-colon of your query.
In order to make your code work fine, you juste have to not concatenate your query :$enroll_sql = '...'; // = not .=
OR
let it concatenated BUT execute the query OUTSIDE your foreach loop (and adding a semi-colon at the end of the SQL query :, "'.$total.'");'; // ; at the end of the query
I wanna thank all contributors especially @WizardNx.
I eventually solved it (Necessity is the mother of creativity):
It appears the checkbox was not assigning unique id
to each row. In fact, if I check row_3
and fill all the inputs, it will insert for row_1
but assign row_3 id
.
Here is what I did:
<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>
I added id
to each input on each row.