我有一個遊戲,其中用戶信息將在遊戲結束後每次完成調查時插入到數據庫中,因此我將這些信息顯示在表中格式在一個網站,下面有一個提交按鈕,所以點擊它將檢查選中和未選中的框並更新數據庫中布爾值的值,但目前我能夠檢查選中的複選框,但沒有選中提交時未在數據庫中更新。如果在提交表單時未選中複選框,則將布爾值更新爲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
您可以使用隱藏的同名輸入來註冊未經檢查的盒子。將它們放在複選框之前,如果選中,將覆蓋隱藏字段中的空值。 – Rasclatt
或者您可以對比兩個數組,一個來自您的問題數組,另一個來自您的答案數組,並獲得差異 – Rasclatt
我認爲'empty($ _POST ['myCheckBox'])'返回true或者選中或未選中複選框。 – FrozenFire