繁体   English   中英

使用 PHP 和 SQL 更新多个复选框

[英]Update multiple check boxes with PHP and SQL

我正在使用以下脚本更新多个输入字段(文本):

前端

<!-- User Input -->
<form method="POST">
 <input name="id[]" value="<?=row['id']?>" hidden>    
 <input type="text" name="text[]" value="<?=$row['text']?>>
** some submit button **
</form>

SQL (PDO) 更新

<!-- Updating every row  -->
$statement = $pdo->prepare("UPDATE textbase SET text= rtrim(:text) WHERE id = :id");
      $statement->bindParam(":text", $text, PDO::PARAM_STR);
      $statement->bindParam(":id", $id, PDO::PARAM_INT);
      foreach ($_POST['text'] as $index => $text) {
      $id = $_POST['id'][$index];
      $statement->execute();
    }

这完美地工作并且没有任何问题。 但是,我现在尝试以相同的方式为每一行(id)添加一个复选框。 我对前端代码进行了以下更改:

问题脚本:

    <!-- User Checkbox -->
        <form method="POST">
        
        <!-- Pass the id for the row -->
        <input name="id[]" value="<?=row['id']?>" hidden>  

<?php // Check if the box needs to be checked as standard
if($row['checkbox'] == '1') { $check = "checked"; } else { $check = ""; } ?>
        
        <!-- If box is not checked send value 0 to avoid empty POST -->
        <input type="checkbox" name="checkbox[]" value="0" hidden>  
        
        <!-- Checkbox -->
        <input type="checkbox" name="checkbox[]" value="1" <?=$check?>>
    
    ** some submit button **
    
    </form>

新品 SQL PDO 查询

 <!-- Updating every row  -->
$statement = $pdo->prepare("UPDATE textbase SET checkbox= rtrim(:checkbox) WHERE id = :id");
      $statement->bindParam(":checkbox", $checkbox, PDO::PARAM_STR);
      $statement->bindParam(":id", $id, PDO::PARAM_INT);
      foreach ($_POST['checkbox'] as $index => $checkbox) {
      $id = $_POST['id'][$index];
      $statement->execute();
    }

我只是使用了相同的 PHP PDO 更新查询,只是将“文本”替换为“复选框”。 这似乎不起作用,我找不到原因。 结果非常混乱。 对我来说,将 id 分配给每一行的结果似乎存在问题。 你们有什么暗示我错过了什么吗?

经过几次测试后,我注意到隐藏复选框的值不会出现在 $_POST 中,原因是您错过了选中的关键字。 只有选中的复选框才会发送到后端

所以你需要做这样的事情


<!-- User Checkbox -->
        <form method="POST">
        
        <!-- Pass the id for the row -->
        <input name="id[]" value="<?=row['id']?>" hidden>  

<?php // Check if the box needs to be checked as standard
if($row['checkbox'] == '1') { $check = "checked"; } else { $check = ""; } ?>
        
        <!-- If box is not checked send value 0 to avoid empty POST -->
        <input type="checkbox" name="checkbox[]" value="0" hidden checked>    <----------
        
        <!-- Checkbox -->
        <input type="checkbox" name="checkbox[]" value="1" <?=$check?>>
    
    </form>

现在你得到了两个值

我不确定您想如何处理结果,但对我来说,更好的选择是为复选框添加索引,以便您可以更轻松地访问它们并防止丢失未选中的值


        <input type="checkbox" name="checkbox[0]" value="0" hidden checked>    
        <!-- Checkbox -->
        <input type="checkbox" name="checkbox[0]" value="1" <?=$check?>>

        <input type="checkbox" name="checkbox[1]" value="0" hidden checked>    
        <!-- Checkbox -->
        <input type="checkbox" name="checkbox[1]" value="1" <?=$check?>>

或者也许更好的解决方案是让后端担心未检查和检查的值。

        <!-- Checkbox -->
        <input type="checkbox" name="checkbox[0]" value="1" <?=$check?>>

        <!-- Checkbox -->
        <input type="checkbox" name="checkbox[1]" value="1" <?=$check?>>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM