Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在数据库中添加复选框值?_Php_Mysql_Html - Fatal编程技术网

Php 如何在数据库中添加复选框值?

Php 如何在数据库中添加复选框值?,php,mysql,html,Php,Mysql,Html,使用cycle创建所需的字符串 <?php if(isset($_REQUEST['submit'])) { $category = mysql_real_escape_string(implode(',', $_POST['category'])); } $ins=mysql_query("insert into fruits (`category`) values ('$category') " ) ; ?> <form name="form

使用cycle创建所需的字符串

<?php 
 if(isset($_REQUEST['submit']))
 {
        $category = mysql_real_escape_string(implode(',', $_POST['category']));

 }

$ins=mysql_query("insert into fruits (`category`) values ('$category') " ) ;

?>

<form name="form1" method="post" action="">
<input type="checkbox"    name="category[]" value="apple" >Apple
<input type="checkbox"    name="category[]" value="orange" >Orange
<input type="checkbox"    name="category[]" value="mango" >Mango
<input type="submit" name="submit" value="submit"  />
</form>

我同意昆汀对你问题的评论——多对多使用桥牌桌是最好的方式,我最近也做了类似的事情,这是最好的解决方案

如果您打算这样做,您只需循环遍历category[]数组并用方括号构建一个字符串,如下所示:

$category = '';

foreach ( $_POST['category'] as $cat )
{
    $category .= '[' . $cat . ']';
}

$ins=mysql_query("insert into fruits (`category`) values ('$category') " ) ;
除非我误解了括号的意义:)


编辑:读一下,(昆汀提供)lmao。在进行这件事之前,你应该考虑可能的安全含义。

< P>我不确定我理解你的意思,但是试试这个< /P>
$str='';
foreach ($_POST['category'] as $val)
{
    $str.='['.$val.']';
}

我希望它能有所帮助。

为什么要在SQL数据库中保存(无论用什么分隔符)列表?使用桥接表建立多对多关系,然后您就可以正确地查询值了。@Quentin,是的,这很有趣。但它只是作者代码中的一个字符串。
$category = mysql_real_escape_string(implode('][', $_POST['category']));
if($category)
   $category='['.$category.']';