/    Sign up×
Community /Pin to ProfileBookmark

Only 1 row is inserted for foreach statement.

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 script is as follows:

<?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;
}
}
}
?>

Thank you.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 27.2018 — Per the manual entry for mysqli_multi_query() (http://php.net/mysqli_multi_query):

**Return Values**

Returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result() first.
[/quote]

To my mind, it would be simpler/cleaner to create a prepared statement, then when looping on the form inputs just binding the relevant form values to the query place-holders, then executing the prepared statement. You would then also gain the benefit of adding protection from SQL injection attacks.
×

Success!

Help @hardsoft spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...