foreach循环重复值php sql

时间:2014-04-27 08:43:34

标签: php html css sql

为什么这个双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>

2 个答案:

答案 0 :(得分:4)

您每次循环使用两次。除此之外,它似乎没有任何问题

答案 1 :(得分:2)

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

   }