mercredi 20 janvier 2016

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

I have a game where users information will be inserted into the database every time they finished a survey after the game, so I display these information in a table format in a website, below there is a submit button, so when clicked it will check for checked and unchecked boxes and update the values of the boolean in the database, but currently I was able to check for checked checkboxes but those that are not checked when submitted are not updated in the database.

here is my code to check for uncheck and checkboxes when submitted

 <?php

    include_once("dcConnect.php");


    if(!empty( $_POST['myCheckBox'] )){

    $strAllUsernameCombined = implode("','", $_POST['myCheckBox']);
    $sql = "UPDATE dcUsers SET dcChecked = 1 WHERE dcID IN ('{$strAllUsernameCombined}')";

    mysqli_query($link, $sql) or exit("result_message=Error");

    } else {

     $strAllUsernameCombined = implode("','", $_POST['myCheckBox']);
     $sql = "UPDATE dcUsers SET dcChecked = 0 WHERE dcID IN ('{$strAllUsernameCombined}')";

    mysqli_query($link, $sql) or exit("result_message=Error");
    }

    ?>

Here is how I display the data from the 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 FROM 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 is the link to my website http://ift.tt/1TFM3la




Aucun commentaire:

Enregistrer un commentaire