通过同时检查多个类别

时间:2013-07-14 17:08:03

标签: php html

我想在我的数据库中获取更多复选框内容。这意味着我有php,java,html和css的类别但我点击php和html,所以它必须是在数据库类别中的同一个数据库中的两个。

当我躺在数据库中时,它看起来像这样:

$kategori = $_POST["kategori"];

用户必须在数据库中选择php和html等类别时的外观:

<input type="checkbox" name="kategori[]" value="<?php echo $tekst;?>"/>
<?php echo $tekst;?>

现在当我把它放入数据库时​​只写“数组”而没有别的,即使我已经为数据库选择了两个类别。

如何将多个类别同时放入数据库?

2 个答案:

答案 0 :(得分:3)

例如,你有2个复选框:

<input type="checkbox" name="kategori[]" value="1"/>
<input type="checkbox" name="kategori[]" value="2"/>

所以当你在提交页面中提交表单时,变量中的数据是$ _POST [“kategori”]结构为:

Array([0] => 1
      [1] => 2
      ); //which you could see by executing the code print_r($_POST["kategori"]);

因此,为了将其放入数据库,您可以将其更改为逗号分隔字符串并插入数据库表中的单个字段

$kategori = implode(",",$_POST["kategori"]); //which will give the output "1,2" when you do echo $kategori;

我希望这可以提供一些帮助

答案 1 :(得分:0)

您希望每个支票都有唯一的ID,您将表单提交到后端,然后您可以通过参考ID检查复选框中每个选中支票的值。