foreach循环复制php sql中的值

foreach循环复制php sql中的值,php,html,css,sql,Php,Html,Css,Sql,为什么这个双foreach循环会复制值 foreach($_POST['studentpoints'] as $value) { foreach($_POST['studentids'] as $valor) { $stmt->bindParam(':studentid', $studentids); $stmt->bindParam(':studentpoints', $points); $s

为什么这个双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 each循环两次。除此之外,$i=0;$ibindParam(“:studentid”,$studentid)似乎没有任何问题;
   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();

   }
$stmt->bindParam(':studentpoints',$points); $stmt->execute(); }
用于($i=0;$ibindParam(“:studentid,$studentid”);
$stmt->bindParam(':studentpoints',$points);
$stmt->execute();
}


您正在调用double foreaches,这就是为什么可以将这两个循环转换为一个循环的原因?可能,我会创建某种类型的
student
对象,它包含整行。然后您只需迭代students并提取相关信息。除此之外,您需要一个
for
循环来获取索引,这将需要更多的错误检查。为什么不能只执行
$stmt->bindParam(':studentid',$valor);
(尽管这不能解决复制问题)“?@Clockwork Muse是的,我试过了,但我仍然得到了你称之为double foreaches的副本,这就是为什么可以将这两个循环转换为一个循环的原因?也许,我会创建某种类型的
student
对象来组成整行。然后你只需在学生身上迭代并提取相关信息。否则,你就可以“我需要一个
循环来获取索引,这需要更多的错误检查。为什么不能只执行
$stmt->bindParam(':studentid',$valor);
(尽管这不能解决复制问题)?@Clockwork Muse是的,我试过了,但我仍然得到了复制是的,我看到了:/我试图做的是将这两个循环转换为一个是的,我看到了:/我试图做的是将这两个循环转换为一个,我尝试了这个,但它给出了一个警告:非法字符串偏移量'studentid'和一个错误致命错误:仅variables可以通过引用在一次中传递,再次感谢您,但是现在出现了一个不同的错误error 1 SQLSTATE[23000]:完整性约束冲突:1048列“studentid”不能为空我修改我的问题如果想查看我尝试的方法,请尝试新的方法。嗯……这是有效的,但对于将单数id作为复数的变量有误导性:
studentid
。谢谢,我正在尝试,但它给出了警告ning:中的字符串偏移量'studentid'非法,出现错误致命错误:只有变量可以通过引用传递。再次感谢,但现在出现了另一个错误错误1 SQLSTATE[23000]:完整性约束冲突:1048列“studentid”不能为Null我修改我的问题如果想查看我尝试的方法,请尝试新方法。嗯……这是有效的,但关于将单数id作为复数的变量,存在误导:
studentid