mardi 2 novembre 2021

How to save the value of checkbox from a loop table in database

It does save the value of the checkbox but not in the correct row. It saves to the first rows. For example, when I checked the second checkbox, but not the first, the value will be saved to the first row, not to the second.

When I do this: enter image description here

This is what happens: enter image description here

<?php
$con = mysqli_connect("localhost","root","","mtc");
  $sql = "SELECT * FROM `june`";
  $result = mysqli_query($con,$sql);

$count=mysqli_num_rows($result);
?>

<?php
if(isset($_POST['Submit']))
{
    $count=count($_POST["id"]);
    
for($i=0;$i<$count;$i++){
    $checkbox = 0;
    if(isset($_POST['claim1'][$i]))
        $checkbox = 1;
$sql1="UPDATE june SET
        name='" . $_POST['name'][$i] . "',
        pay1='" . $_POST['pay1'][$i] . "',
        claim1='" . $checkbox . "'
        WHERE id='" . $_POST['id'][$i] . "'";
$result1=mysqli_query($con, $sql1);
}
}

?>
<main>
    <div class="content-wrapper">
        <div class="container-fluid">
            <div class="row">
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form method="post" >
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">

<tr>
<td align="center"><strong>ID</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Pay</strong></td>
<td align="center"><strong>Claim</strong></td>
</tr>

<?php
while($rows=mysqli_fetch_array($result, MYSQLI_ASSOC)){
?>


<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>">
</td>

<td align="center">
<input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>

<td align="center">
<input name="pay1[]" type="text" id="pay1" value="<?php echo $rows['pay1']; ?>">
</td>
<td align="center">
<input class="form-check-input" type="checkbox" id="claim1" name="claim1[]" <?php if($rows['claim1']==1) echo 'checked' ?>>
</td>
</tr>

<?php
}
?>

</table>
<input type="submit" name="Submit" value="Submit">

</td>
</tr>
</form>
</table>

</div>
</div>
</div>
</main>



Aucun commentaire:

Enregistrer un commentaire