【问题标题】:how to insert multi-checkbox value if they are checked in db in php如果它们在 php 中的 db 中被选中,如何插入多复选框值
【发布时间】:2016-05-14 19:47:18
【问题描述】:

我想将在 db 中选中的复选框的值保存在 php.ini 中。这里有很多复选框,我想保存所选复选框的值。我该如何处理?

<html>

  <body>
     <input type="checkbox" name="category[]" value="1">
     <input type="checkbox" name="category[]" value="2">
     <input type="checkbox" name="category[]" value="3">
      .
      .
      .
     <input type="checkbox" name="category[]" value="1000">
  </body>

</html>

【问题讨论】:

  • 将所有输入放在form标签中,当您提交表单时,您将获得一个仅包含所有选中复选框值的数组
  • 输入表单并提交..您将获得已选中的复选框数组,然后您可以将它们保存在逗号分隔的一列中或使用循环保存在其他表中

标签: php database checkbox


【解决方案1】:

在你的 PHP 代码中这样做:

<?php 
$categories = $_POST['category'];
for($i=0;$i<count($categories);$i++){
     $category = $categories[$i];    
    //insert query should comes here
    mysql_query("insert into table value($category)");
}
?>

【讨论】:

    【解决方案2】:

    创建一个表单并将它们全部放入表单中。当有人提交表单时,您将获得一组具有 category KEY 的值。

    所以您的 HTML 代码将如下所示,假设您将使用 POST 方法。

    <html>
    
      <body>
         <form method="POST" action="">
         <input type="checkbox" name="category[]" value="1">
         <input type="checkbox" name="category[]" value="2">
         <input type="checkbox" name="category[]" value="3">
          .
          .
          .
         <input type="checkbox" name="category[]" value="1000">
         <input type="submit" value="Submit"/>
         </form>
      </body>
    
    </html>
    

    你可以像这样在 PHP 中获取值,

    $category_values=$_POST["category"];
    print_r($category_values); // for testing purpose, to know the selected checkboxes.
    

    【讨论】:

      【解决方案3】:

      这样试试

      <?php 
      $categories = $_POST['category'];
      
      foreach($categories as $row) {
      
       //insert query 
      
      "insert into table(column_name) values('$row')";
      
      }
       ?>
      

      【讨论】:

        猜你喜欢
        • 2021-04-03
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 2021-03-06
        • 2016-03-04
        • 2014-10-15
        相关资源
        最近更新 更多