Why this double foreach loop duplicates values
foreach($_POST['studentpoints'] as $value) {
foreach($_POST['studentids'] as $valor) {
$stmt->bindParam(':studentid', $studentids);
$stmt->bindParam(':studentpoints', $points);
$studentids = $valor;
$points = $value;
$stmt->execute();
}
This code don't duplicate the values, but only read the first id for students
foreach($_POST['studentpoints'] as $value) {
foreach($_POST['studentids'] as $valor) {
$studentids = $valor;
}
$stmt->bindParam(':studentid', $studentids);
$stmt->bindParam(':studentpoints', $points);
$studentids = $valor;
$points = $value;
$stmt->execute();
}
Table with data from database
<?php foreach($rowstudents as $row): ?>
<tr>
<th><input type="hidden" name="studentids[]" value="<?php echo ' ' . htmlentities($row['studentid'], ENT_QUOTES, 'UTF-8') . ' ';?>" />
<?php echo '' . htmlentities($row['studentid'], ENT_QUOTES, 'UTF-8') . '';?></th>
<th><?php echo '' . htmlentities($row['fname'], ENT_QUOTES, 'UTF-8') . '';?></th>
<th><?php echo '' . htmlentities($row['lname'], ENT_QUOTES, 'UTF-8') . '';?></th>
<th><input type="text" name="studentpoints[]" value="<?php echo '' . htmlentities($row['studentpoints'], ENT_QUOTES, 'UTF-8') . '' ?>"></th>
</tr>
<?php endforeach; ?>
</table>
You are using a for each loop twice. Apart from that it doesn't seem there is any problem
for ($i=0; $i<count($_POST['studentpoints']); $i++) {
$studentids = $_POST['studentid'][$i];
$points = $_POST['studentpoints'][$i];
$stmt->bindParam(':studentid', $studentids);
$stmt->bindParam(':studentpoints', $points);
$stmt->execute();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.