【发布时间】:2016-04-27 04:05:34
【问题描述】:
我有一个游戏,每次游戏结束后,用户的信息都会被插入到数据库中,所以我在网站中以表格的形式显示这些信息,下面有一个提交按钮,所以当点击它时将检查选中和未选中的框并更新数据库中布尔值的值,但目前我能够检查选中的复选框,但提交时未选中的复选框不会在数据库中更新。
这是我提交时检查取消选中和复选框的代码
<?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>
【问题讨论】:
-
您可以使用隐藏的同名输入来注册未选中的框。将它们放在复选框之前,如果选中,将覆盖隐藏字段中的空值。
-
或者你可以对比两个数组,一个来自你的 questions 数组,一个来自你的 answers 数组并得到区别
-
我认为
empty( $_POST['myCheckBox'] )是否选中复选框都会返回true。
标签: php html mysql forms checkbox