【问题标题】:How to update boolean values to 0 if checkboxes are not checked when form is submitted如果提交表单时未选中复选框,如何将布尔值更新为 0
【发布时间】: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>  

这是我网站的链接 http://forstoringdata.com/default.php

【问题讨论】:

  • 您可以使用隐藏的同名输入来注册未选中的框。将它们放在复选框之前,如果选中,将覆盖隐藏字段中的空值。
  • 或者你可以对比两个数组,一个来自你的 questions 数组,一个来自你的 answers 数组并得到区别
  • 我认为empty( $_POST['myCheckBox'] ) 是否选中复选框都会返回true。

标签: php html mysql forms checkbox


【解决方案1】:

如果未选中,提交后的 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");
    }
}

【讨论】:

  • 感谢回复,mysql语句有什么需要改的吗?
  • 嘿,我在尝试代码后得到了这个错误警告:implode(): Invalid arguments pass in /home/forsto5/public_html/default3.php
  • 未选中复选框时布尔值仍不会变为 0.. :(( 但选中时变为 1。
  • 我的答案已更新。您是否再次尝试使用我上面的新更新答案?
  • 是的..,还有隐藏的复选框
猜你喜欢
  • 2015-04-13
  • 2015-09-30
  • 2013-10-04
  • 1970-01-01
  • 2016-03-14
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
相关资源
最近更新 更多