提交后保持复选框数组选中 - 不起作用

Keep checkbox array checked after submit-not working

在提出我的问题之前,我参考并尝试了此类问题线程中提供的许多解决方案,因为其中 none 对我来说工作正常。 我有一个 mysql table 的学生存储学生姓名、分数。 我正在使用 while 循环从数据库中检索该数据。

$query="select * from student";
$rs=  mysql_query($query) or die(mysql_error());


<?php if(mysql_num_rows($rs)){ ?>
  <table border="5" cellspacing="5" width="50%" align="center">
      <tr>
          <th>No</th>
          <th>Name</th>
          <th>Marks</th>
          <th>Operation</th>
          <th>  <input type ="submit" name="delete" value="Delete"></th>
      </tr>
      <?php


 while($row=mysql_fetch_array($rs))
          {


?>
      <tr>
           <th><?php echo $row['rollno']; ?></th>
          <th><?php echo $row['name']; ?></th>
          <th><?php echo $row['marks']; ?></th>
                      <th><a href="AllOperation.php?&no=<?php echo $row['rollno']; ?>&name=<?php echo $row['name']; ?>&marks=<?php echo $row['marks']; ?>">View</a></th>
          <th><input type="checkbox" name="check[]" value="<?php echo $row['marks']; ?>" <?php if(isset($_POST['check']))  if (in_array($row['marks'], $_POST['check'])) echo "checked='checked'"; ?> /></th>
      </tr>
      <tr>
      <?php  } ?>
<input type ="submit" name="total" value="total">

我正在生成所有学生的总分

if(isset($_POST['total']))


{                        $t=0;
                     foreach($_REQUEST['check'] as $val)
                     {                             
                         $t=$t+$val;                             
                     }
               echo "    total : ".$t;


 }

现在的问题是,当我第一次 运行 程序时,它显示了 5 个学生的信息,然后我选择了前两个复选框并按下 "total" 按钮以生成总数。因此它会正确显示总数并选中这两个复选框。 但是当我选中第三个复选框并按下 "total" 按钮时 它显示了三个选中复选框标记的总和,但它显示了第四个或最后一个选中的复选框,即使我没有选中它也是如此。 那么为什么会这样。

我看到的问题: if (in_array($row['marks'], $_POST['check']) echo "checked='checked'

当您提交时,它总是检查具有相同标记的复选框

我认为您应该使用另一个独特的列,例如 $row['rollno']; 而不是 $row['marks' ]