为什么这个双重foreach循环会重复值

 foreach($_POST['studentpoints'] as $value) {
        foreach($_POST['studentids'] as $valor) {
            $stmt->bindParam(':studentid', $studentids);
            $stmt->bindParam(':studentpoints', $points);
            $studentids = $valor;
            $points = $value;
            $stmt->execute();
        }


此代码不重复值,而仅读取学生的第一个ID

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();

}


具有数据库数据的表

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

最佳答案

   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();

   }

关于php - foreach循环重复值php sql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23321236/

10-12 12:41