使用php和mysql将多个复选框值存储到数据库

时间:2022-09-25 16:39:20

I want to store the multiple checkbox values to store in a single field. I use that link http://www.mindfiresolutions.com/Storing-array-data-to-MySQL-using-PHP-1296.php. But i dont get the result.Give Help to find that problem.

我想存储多个复选框值以存储在单个字段中。我使用的链接http://www.mindfiresolutions.com/Storing-array-data-to-MySQL-using-PHP-1296.php。但我没有得到结果。给帮助找到这个问题。

2 个解决方案

#1


1  

Set your column as 'set' (specify the all possible values.) data type and than run the below query.

将列设置为“set”(指定所有可能的值。)数据类型,然后运行以下查询。

$comma_separated = implode(",", $values);
$insert_query = "INSERT INTO TABLE_NAME(col_name) VALUES('$comma_separated')";
$result_insert = mysql_query($insert_query);

I hope this will solve your problem.

我希望这能解决你的问题。

#2


0  

I hope this will helpful

我希望这会有所帮助

<html>
<body>
<form action="" method="post">
<p><input type="checkbox" name="color[]" value="red" />Red</p>
<p><input type="checkbox" name="color[]" value="blue" />Blue</p>
<p><input type="checkbox" name="color[]" value="orange" />orange</p>
<input type="submit" value="submit" name="sub" />
</form>

<?php

if(isset($_POST['sub']))
{
    mysql_connect("localhost","root","") or die("Server Could not be connected");
    mysql_select_db("gobinath") or die("database connection problam");
    $color=implode(',',$_POST['color']);

    mysql_query("insert into mcheck values('','$color')") or die("insert problam");

}
?>
</body>
</html>

#1


1  

Set your column as 'set' (specify the all possible values.) data type and than run the below query.

将列设置为“set”(指定所有可能的值。)数据类型,然后运行以下查询。

$comma_separated = implode(",", $values);
$insert_query = "INSERT INTO TABLE_NAME(col_name) VALUES('$comma_separated')";
$result_insert = mysql_query($insert_query);

I hope this will solve your problem.

我希望这能解决你的问题。

#2


0  

I hope this will helpful

我希望这会有所帮助

<html>
<body>
<form action="" method="post">
<p><input type="checkbox" name="color[]" value="red" />Red</p>
<p><input type="checkbox" name="color[]" value="blue" />Blue</p>
<p><input type="checkbox" name="color[]" value="orange" />orange</p>
<input type="submit" value="submit" name="sub" />
</form>

<?php

if(isset($_POST['sub']))
{
    mysql_connect("localhost","root","") or die("Server Could not be connected");
    mysql_select_db("gobinath") or die("database connection problam");
    $color=implode(',',$_POST['color']);

    mysql_query("insert into mcheck values('','$color')") or die("insert problam");

}
?>
</body>
</html>