提交表单onchange复选框并更新mysql db

<?php
if(isset($_POST['chkStatus'])){
    for ($i=0;$i<count($_POST['chkStatus']);$i++) {
        $count = count($_POST['chkStatus']);
        list($txtClientId, $txStatusValue) = explode('-', $_POST['chkStatus'][$i], 2);
        $stmtChkStatus=$con->query("SELECT id,status FROM users WHERE id=$txtClientId");
        $SRow = $stmtChkStatus->fetch();
        if($SRow['status']!=$txStatusValue) {
            if($txStatusValue==0) $stmtStatus=$con->query("UPDATE users SET status='0' WHERE id=$txtClientId");
            else $stmtStatus=$con->query("UPDATE users SET status='1' WHERE id=$txtClientId");
        }
    }
}
?>

<html>
    <body>
        <dl>
            <?php $stmtUsers=$con->query("SELECT id,status FROM users");
            while($UsersRow = $stmtUsers->fetch()){
                echo "<dt>$UsersRow[name]</dt>
                <dd><input type='checkbox' name='chkStatus[]' value='$UsersRow[id]-$UsersRow[status]'";
                if($CRow['status']==0) echo " checked";
                echo " onchange='this.form.submit()'>
                </dd>";
            ?>
        </dl>
    </body>
</html>

The issue with my code is that it only execute the checked checkboxes. I need a code that changes the status in SQL DB of the checkbox that i click(onchange).

Instead of looping through $_POST['chkStatus'] you could fetch all possible values with $con->query("SELECT id,status FROM users") and loop over those values. You can check then, if $_POST['chkStatus'] is set for the according value(s).

At a moment you send all checkbox values to the server. You may send only interested values. Quick and dirty solution:

  1. split you checkboxes into individual forms and submit only form you need:

    while($UsersRow = $stmtUsers->fetch()){
            echo "<dt>$UsersRow[name]</dt>
       <form .... >  <!-- Here required attributes  -->
            <dd><input type='checkbox' name='chkStatus[]' value='$UsersRow[id]-$UsersRow[status]'";
            if($CRow['status']==0) echo " checked";
            echo " onchange='this.form.submit()'>
       </form>
            </dd>";
    
  2. In inclick attribute take id or name of checkbox and add to request.