如果未选中复选框,Php 会更新数据库

问题描述 投票:0回答:2

所以我有一个动态生成的表

<tr>
    <td><?php echo $row['RequestID']?></td>
    <td><?php echo $row['FirstName'] . " " . $row['LastName']?></td>
    <td><?php echo $row['DateRequested']?></td>
    <td><?php echo $row['DateNeeded']?></td>
    <td><?php echo $row['TotalPrice']?></td>
    <td><?php echo $row['Company']?></td>
    <td><?php echo $row['Account']?></td>
    <td><?php echo $row['Brand']?></td>
    <td><?php echo $quantity?></td>
    <td><input type="checkbox" name="bill[]" value=<?php echo '"' . $row['RequestID'] . '"'; if($row['BillBack'] == "1") {echo "checked='checked'"; } ?></td>
</tr>

我想在选中或取消选中一行时更新数据库。

if(isset($_POST['submit'])){//to run PHP script on submit
    if(!empty($_POST['bill'])){
    // Loop to store and display values of individual checked checkbox.
    foreach($_POST['bill'] as $selected){
    echo "hello";
    $sql2 = "UPDATE Requests SET BillBack = '1' WHERE RequestID = '$selected'";
    $result2 = $conn->query($sql2);

    }} elseif(empty($_POST['bill'])){
    // Loop to store and display values of individual checked checkbox.
    foreach($_POST['bill'] as $selected){
    echo "hello";
    $sql2 = "UPDATE Requests SET BillBack = '0' WHERE RequestID = '$selected'";
    $result2 = $conn->query($sql2);
    }}

         }

现在。 if 语句的作用就像一个魅力。对于提交中的每个选中的复选框,该记录都会更新。问题来自于取消选中复选框。正如您所看到的,通过查找不为空的内容来找到复选框。但是,当我删除该感叹号时,它并没有有效地起到相反的作用。它也不起作用,因为它有自己的 if 语句、直接的 else 语句以及检查 !isset。这是因为输入被视为检查的方式吗?有语法错误吗?我已经做了大约 5 个小时的错误检查、谷歌搜索,但我无法弄清楚。 谢谢大家

php mysql checkbox
2个回答
1
投票

非常简单:只需将所有条目更新为 0,然后将选中的条目设置为 1。

//to run PHP script on submit
if(isset($_POST['submit'])){    
    // Setting everyone to 0
    $sql2 = "UPDATE Requests SET BillBack = '0'";
    $result2 = $conn->query($sql2);

    // Setting only those checked to 1
    if(!empty($_POST['bill'])){
        // Loop to store and display values of individual checked checkbox.
        foreach($_POST['bill'] as $selected){
            echo "hello";
            $sql3 = "UPDATE Requests SET BillBack = '1' WHERE RequestID = '$selected'";
            $result3 = $conn->query($sql3);
        }
    }
}

编辑:

请注意,在 foreach 内进行查询可能会导致服务器过载,为避免这种情况,您可以存储要更新的条目 id,并仅在 foreach 外的一个查询中使用它:

//to run PHP script on submit
if(isset($_POST['submit'])){
    // Setting everyone to 0
    $sql2 = "UPDATE Requests SET BillBack = '0'"; // Maybe add a WHERE clause, like BizzyBob pointed out
    $result2 = $conn->query($sql2);

    // Setting only those checked to 1
    if(!empty($_POST['bill'])){

        // Initialize an array of selected ids
        $selected_ids = array(); 

        // Loop to store individual checked checkboxes on array        
        foreach($_POST['bill'] as $selected){
            $selected_ids[] = $selected;
        }

        // Get the ids separated by comma
        $in_clause = implode(", ", $selected_ids);

        // Do only one sql query
        $sql3 = "UPDATE Requests SET BillBack = '1' WHERE RequestID in (".$in_clause.")";
        $result3 = $conn->query($sql3);
    }
}

0
投票
            <input type="hidden" name="id" value="<?php echo $instructor['id']; ?>">

            <label>Name: </label>
            <input type="text" name="name" value="<?php echo $instructor['name']; ?>" required><br><br>

            <label>E-mail:</label>
            <input type="email" name="email" value="<?php echo $instructor['email']; ?>" required><br><br>
            
            <!-- Designation -->
            <label>Designation:</label>
            <select name="designation" required>
                <option value="lecturer" <?php if ($instructor['designation'] == 'lecturer') echo 'selected'; ?>>Lecturer</option>
                <option value="senior lecturer" <?php if ($instructor['designation'] == 'senior lecturer') echo 'selected'; ?>>Senior Lecturer</option>
                <option value="demonstrator" <?php if ($instructor['designation'] == 'demonstrator') echo 'selected'; ?>>Demonstrator</option>
                <option value="attendant" <?php if ($instructor['designation'] == 'attendant') echo 'selected'; ?>>Attendant</option>
            </select><br><br>

            <label>Salary:</label>
            <input type="number" name="salary" value="<?php echo $instructor['salary']; ?>" step="0.01" required><br><br>

            <!-- Role checkboxes -->
            <label>Role:</label>
            <?php $roles = explode(',', $instructor['role']); ?>
            <input type="checkbox" name="role[]" value="teaching" <?php if (in_array('teaching', $roles)) echo 'checked'; ?>>Teaching
            <input type="checkbox" name="role[]" value="assisting" <?php if (in_array('assisting', $roles)) echo 'checked'; ?>>Assisting
            <input type="checkbox" name="role[]" value="supervising" <?php if (in_array('supervising', $roles)) echo 'checked'; ?>>Supervising<br><br>
            
            <!-- Submit button -->
            <input type="submit" value="Update">
        </form>
© www.soinside.com 2019 - 2024. All rights reserved.