带行的多个复选框更新

时间:2017-07-22 14:13:52

标签: php mysql forms sql-update

i have this form

如何使用复选框按行更新此记录?该复选框保存我从上一个表单发送的值。

this would be the database

这是对的吗?

while ($row = mysqli_fetch_array($result)){

。 。 。

    <td><center>
    <input type="hidden" name="matric[]" value="<?php echo $matric?>"/><p>
      <input class="w3-check" type="checkbox" value='$hour' name="unattend<?php echo $matric?>[]">
      <label> Unattend</label></p> </center></td>
    <td><center>
    <input type="hidden" name="matric[]" value="<?php echo $matric?>"/><p>
      <input class="w3-check" type="checkbox" value='$hour' name="mc<?php echo $matric?>[]">
      <label> I have MC </label></p> </center></td>   }

如何更新mysql?

1 个答案:

答案 0 :(得分:0)

这是表格

<?php
$i = 1;
while ($row = mysqli_fetch_array($result)) {
    $id=$row['matric_number'];
?>

 <form method="POST" action="attend.php">
<tr>
<td><center><?php echo $i; ?></center></td>
<td><center><?php echo $row['student_name']; ?></center></td>
<td><center><?php echo $row['matric_number']; ?></center></td>
<td><center>
<p>
<input class="w3-check" name="unattend[]" type="checkbox" id="unattend[]" value="<?php echo $id; ?>">
 <label> Unattend</label></p> </center></td>
<td><center>
<p>
  <input class="w3-check" name="mc[]" type="checkbox" id="mc[]" value="<?php echo $id; ?>">
  <label> I have MC </label></p> </center></td>
<td><center>
<?php echo "<a href=student.php?id1=".$row['matric_number'].">"."Details"; ?></center></td>



</tr>

<?php
$i++;
}


mysqli_close ($link);
?>
</table>

按行更新数据的php是

<?php
if (isset($_POST['unattend'])) {

$hour=$_POST['hour'];
$id=$_POST['unattend'];
$N = count($id);

for($i=0; $i < $N; $i++)
{
$select = mysqli_query($link,"SELECT * FROM pbu WHERE matric_number='$id[$i]'")or die(mysql_error());
while ($row = mysqli_fetch_array($select)) {

$value=$row['unattend'];
$total=$value+$hour;
$result = mysqli_query($link,"UPDATE pbu SET unattend='$total' WHERE matric_number='$id[$i]'")or die(mysql_error());
echo "unattend is updated for matric number ".$id[$i]."<br>";



}
}

}
if (isset($_POST['mc'])){
$id1=$_POST['mc'];
$N = count($id1);
for($i=0; $i < $N; $i++)
{
$select = mysqli_query($link,"SELECT * FROM pbu WHERE matric_number='$id[$i]'")or die(mysql_error());

while ($row = mysqli_fetch_array($select)) {

$values=$row['mc'];
$totals=$values+$hour;
$results = mysqli_query($link,"UPDATE pbu SET mc='$totals' WHERE matric_number='$id1[$i]'")or die(mysql_error());
echo "mc is updated for ".$id1[$i]."<br>";

}

}
}
else{
echo "nothing inserted";
}


 ?>