2016-01-21 23 views
2

我有一個遊戲,其中用戶信息將在遊戲結束後每次完成調查時插入到數據庫中,因此我將這些信息顯示在表中格式在一個網站,下面有一個提交按鈕,所以點擊它將檢查選中和未選中的框並更新數據庫中布爾值的值,但目前我能夠檢查選中的複選框,但沒有選中提交時未在數據庫中更新。如果在提交表單時未選中複選框,則將布爾值更新爲0

這裏是我的代碼檢查取消選中和複選框提交時

<?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"); 
    } 

    ?> 

這裏是我從數據庫

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

這裏顯示的數據是鏈接到我的網站 http://forstoringdata.com/default.php

+0

您可以使用隱藏的同名輸入來註冊未經檢查的盒子。將它們放在複選框之前,如果選中,將覆蓋隱藏字段中的空值。 – Rasclatt

+0

或者您可以對比兩個數組,一個來自您的問題數組,另一個來自您的答案數組,並獲得差異 – Rasclatt

+0

我認爲'empty($ _POST ['myCheckBox'])'返回true或者選中或未選中複選框。 – FrozenFire

回答

2

提交後的HTML複選框將在未選中時丟失值。你需要添加一個隱藏的輸入BEFORE那個複選框

<input type='hidden' name='myCheckBox[0]' value='0'> 

<input type='checkbox' id='". $row["dcID"] ."' name='myCheckBox[]' value='". $row["dcID"] ."'".(($row["dcChecked"]) ? 'checked="checked"':"")." > 

不過我猜你要爲每個選中框默認爲0:閱讀您的網站後,下一個方法是首選:

<input type='hidden' name='myCheckbox[". $row['dcID'] ."]' value='0'>  
<input type='checkbox' name='myCheckBox[". $row['dcID'] ."]' 
    value='1'". ($row['dcChecked'] ? ' checked' : '') ."> 

這裏是更新MySQL位的PHP:

if (isset($_POST['myCheckBox'])) { 
    $yes = $no = array(); 
    foreach ($_POST['myCheckBox'] as $dcID => $dcChecked) { 
     if ($dcChecked) { 
      $yes[] = $dcID; 
     } else { 
      $no[] = $dcID; 
     } 
    } 
    if (count($yes)) { 
     $sql = 'UPDATE dcUsers SET dcChecked = 1 WHERE dcID IN ('. implode(',', $yes) .')'; 
     mysqli_query($link, $sql) or exit("result_message=Error"); 
    } 
    if (count($no)) { 
     $sql = 'UPDATE dcUsers SET dcChecked = 0 WHERE dcID IN ('. implode(',', $no) .')'; 
     echo $sql; //delete this line after debugging 
     mysqli_query($link, $sql) or exit("result_message=Error"); 
    } 
} 
+0

感謝您的回覆,有什麼我應該改變爲MySQL的聲明? – Nigel

+0

嗨,我在試用代碼後得到這個錯誤警告:implode():在/home/forsto5/public_html/default3.php傳遞的無效參數 – Nigel

+0

對不起,我的錯字。現在更新 – SIDU

相關問題