【问题标题】:Update multiple check boxes with PHP and SQL使用 PHP 和 SQL 更新多个复选框
【发布时间】:2022-01-03 13:32:38
【问题描述】:

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

前端

<!-- 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 分配给每一行的结果似乎存在问题。你们有什么提示我错过了什么吗?

【问题讨论】:

  • 您是否更新了 PDO 设置中的 foreach 以包含正确的 POST 字段?
  • 是的,这就是我的意思:我只是使用了相同的 PHP PDO 更新查询,只是将“文本”替换为“复选框”。我观察到,每次我检查列表中的一个框时,都会检查第一个项目。如果我选中列表中的另一个框,则会选中第二个项目。停用是不可能的。 (我添加了更新后的查询以便更好地理解)。
  • 您认为未选中的复选框不会出现在 $_POST 中吗?例如,如果您有 3 个复选框输入并且您选中了其中的 2 个,那么在提交按钮之后您的 $_POST['checkbox'] 有 2 个成员!
  • 是的,这就是我要避免的:
  • 你的 $_POST['checkbox'] 是什么样的?你能给我们看一个 var_dump 什么的吗?

标签: php sql input checkbox pdo


【解决方案1】:

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

所以你需要做这样的事情


<!-- 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?>>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多