0
<?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).

chris85
  • 23,846
  • 7
  • 34
  • 51
GaLaTaS
  • 1
  • 6
  • For future postings please see https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks, don't use HTML for formatting of code. – chris85 Apr 05 '17 at 15:02
  • 1
    Your code is vulnerable to SQL injection attacks. You should use [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) or [PDO](http://php.net/manual/en/pdo.prepared-statements.php) prepared statements as described in [this post](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 05 '17 at 15:13
  • whats the problem? – Omi Apr 05 '17 at 15:32

2 Answers2

0

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).

chris85
  • 23,846
  • 7
  • 34
  • 51
Sebastian
  • 416
  • 1
  • 6
  • 19
  • i want to avoid the query running through all the records.. how can i do it to run the sql where id= the onchange checkbox? – GaLaTaS Apr 05 '17 at 15:08
0

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.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59