更新表与PHP中的多个复选框

时间:2015-10-12 07:17:47

标签: php

我在表单中有多个复选框,当单击提交按钮时,数据库中的表已更新,当选中复选框时,此代码是正确的但是如果未选中复选框则表格未更新!

<form method="post">
<?php
$qr2=$mysqli->query("SELECT `action_id`,`enable` FROM `role`");
    for($i=0;$row2=$qr2->fetch_object();$i++){
?>
    <input type="checkbox" <?php if($row2->enable == '1'){ ?> checked <?php } ?> id="upc" name="upc[]" value="<?php echo $row2->action_id; ?>">
<?php
    }
?>
<input type="submit" id="up" name="up" value="update">
<?php update_role(); ?>
</form>

<?php
function update_role(){
    if(isset($_POST['up'])){
        foreach($_POST['upc'] as $check) {
            if(isset($_POST['upc'])) {
                $mysqli->query("UPDATE `role` SET `enable`='1' WHERE `action_id`='$check'");
            }
            else{
                $mysqli->query("UPDATE `role` SET `enable`='0' WHERE `action_id`='$check'");
            }
        }
    }
}
?>

编辑:

在复选框附近使用文本框并显示此角色的“价格”,我想更新此值的表格:

<form method="post">
    <?php
    $qr2=$mysqli->query("SELECT `action_id`,`enable`,`price`,`id` FROM `role`");
        for($i=0;$row2=$qr2->fetch_object();$i++){
    ?>
        <input type="checkbox" <?php if($row2->enable == '1'){ ?> checked <?php } ?> id="upc" name="upc[]" value="<?php echo $row2->action_id; ?>">
        <input type="text" class="nw-prc" value="<?php echo $row2->price; ?>" name="price[<?php echo $row2->id; ?>]">
    <?php
        }
    ?>
    <input type="submit" id="up" name="up" value="update">
    <?php update_role(); ?>
    </form>

1 个答案:

答案 0 :(得分:1)

根据您的上一条评论,我认为您希望使用所选框中的所有值更新数据库,即使它们未被选中(由用户取消选择)

我用一个例子更新了我的答案。你可以通过多种方式做到这一点,这只是你如何做到这一点的一个例子。

<html>
    <head></head>
    <body>
        <form method="post">
            <?php
            // Configure your database connection
            $servername = "";
            $username = "";
            $password = "";
            $dbname = "";
            $mysqli = new mysqli($servername, $username, $password, $dbname);

            if ($mysqli->connect_error) {
                die("Connection failed: " . $mysqli->connect_error);
            }

            $role = update_role();

            foreach ($role as $actionId => $enable) {
                echo sprintf(
                    '<input type="checkbox" %s id="upc" name="upc[]" value="%s">',
                    $enable == '1' ? 'checked="checked"' : "",
                    $actionId
                );
            }
            ?>
            <input type="submit" id="up" name="up" value="update">
        </form>

<?php
/**
 * Return an array which contains the configured role settings.
 * In the format array($actionId => $enable)
 *
 * For example:
 *
 *  array(
 *      1 => '0',
 *      422 => '1'
 *  );
 *
 * @return array
 */
function update_role()
{
    global $mysqli;

    $qr2 = $mysqli->query("SELECT `action_id`,`enable` FROM `role`");
    $role = [];

    // Put the role from the database in an array.
    for ($i=0; $row2=$qr2->fetch_object(); $i++) {
        $role[$row2->action_id] = $row2->enable;
    }

    if (isset($_POST['up'])) {
        $isUpcSelected = isset($_POST['upc']) ? true : false;

        // Loop the role array and make the default 'enable' value '0' because on the server you will only receive
        // which checkboxes are selected in the $_POST array. This means that $isUpcSelected can be false on a POST.
        // But we also want to update the select boxes that are not selected/deselected, which they are now by default.
        foreach ($role as $actionId => $enable) {
            $role[$actionId] = '0';
        }

        // Now if there are selected checkboxes, then get them from the $_POST array and set their value to '1'.
        if ($isUpcSelected) {
            foreach ($_POST['upc'] as $checked) {
                if (array_key_exists($checked, $role)) {
                    $role[$checked] = '1';
                }
            }
        }

        // Then update the database with the values from the $role array.
        foreach ($role as $actionId => $enable) {
            $mysqli->query("UPDATE `role` SET `enable`='$enable' WHERE `action_id`='$actionId'");
        }
    }

    return $role;
}
?>
    </body>
</html>