php - How to update boolean values to 0 if checkboxes are not checked when form is submitted -


i have game users information inserted database every time finished survey after game, display these information in table format in website, below there submit button, when clicked check checked , unchecked boxes , update values of boolean in database, able check checked checkboxes not checked when submitted not updated in database.

here code check uncheck , checkboxes when submitted

 <?php      include_once("dcconnect.php");       if(!empty( $_post['mycheckbox'] )){      $strallusernamecombined = implode("','", $_post['mycheckbox']);     $sql = "update dcusers set dcchecked = 1 dcid in ('{$strallusernamecombined}')";      mysqli_query($link, $sql) or exit("result_message=error");      } else {       $strallusernamecombined = implode("','", $_post['mycheckbox']);      $sql = "update dcusers set dcchecked = 0 dcid in ('{$strallusernamecombined}')";      mysqli_query($link, $sql) or exit("result_message=error");     }      ?> 

here how display data database

 <p> <form action="default3.php" method="post"</form>             <?php     include_once("dcconnect.php");      $dcdata = "select dcchecked, dcid, dcservername, dcserveroc, dcserverage, dcservergender, dcservermarital, dcservercode, dcserverpoints dcusers";      $result = $link->query($dcdata);      if($result->num_rows >0){         echo"<table><tr><th>redeem</th><th>id</th><th>name</th><th>occupation</th><th>age group</th><th>gender</th><th>marital status</th><th>code</th><th>points</th></tr>";         while($row = $result->fetch_assoc()){             echo "<tr><td></input><input type='checkbox' id='". $row["dcid"] ."' name='mycheckbox[]' value='". $row["dcid"] ."'".(($row["dcchecked"]) ? 'checked="checked"':"")." ></input></td><td>". $row["dcid"] ."</td><td>". $row["dcservername"] ."</td><td>". $row["dcserveroc"] ."</td><td>". $row["dcserverage"] ."</td><td>". $row["dcservergender"] ."</td><td>". $row["dcservermarital"] ."</td><td>". $row["dcservercode"] ."</td><td>". $row["dcserverpoints"] ."</td></tr>";              }         echo "</table>" ;          }else{             echo"no results";          }         $link->close();        ?></p>            <input type="submit" name="submitter" value="save">            </form>   

here link website http://forstoringdata.com/default.php

html checkbox after submit lost value if not checked. need add hidden input before checkbox

<input type='hidden' name='mycheckbox[0]' value='0'>  <input type='checkbox' id='". $row["dcid"] ."' name='mycheckbox[]' value='". $row["dcid"] ."'".(($row["dcchecked"]) ? 'checked="checked"':"")." > 

however guess want default 0 each unchecked boxes: after reading website, next method preferred:

<input type='hidden' name='mycheckbox[". $row['dcid'] ."]' value='0'>        <input type='checkbox' name='mycheckbox[". $row['dcid'] ."]'      value='1'". ($row['dcchecked'] ? ' checked' : '') ."> 

and here php update mysql bit:

if (isset($_post['mycheckbox'])) {     $yes = $no = array();     foreach ($_post['mycheckbox'] $dcid => $dcchecked) {         if ($dcchecked) {             $yes[] = $dcid;         } else {             $no[]  = $dcid;         }     }     if (count($yes)) {         $sql = 'update dcusers set dcchecked = 1 dcid in ('. implode(',', $yes) .')';         mysqli_query($link, $sql) or exit("result_message=error");     }     if (count($no)) {         $sql = 'update dcusers set dcchecked = 0 dcid in ('. implode(',', $no) .')';         echo $sql; //delete line after debugging         mysqli_query($link, $sql) or exit("result_message=error");     } } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -