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

时间:2016-12-29 21:47:09

标签: php mysql checkbox

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

<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个小时的错误检查,谷歌搜索,我无法弄明白。 谢谢你们

1 个答案:

答案 0 :(得分:0)

非常简单:只需将所有条目更新为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);
    }
}