Sunday, July 26, 2009

Entering multiple values with checkbox into mysql

In this post I will discuss how I can pass multiple values of checkbox into mysql database. If we try to insert checkbox data in normal way then we will see only one value of it is inserted. Also, there is exactly one field to hold multiple checkboxes data unless you want to insert multiple records into database. Now the question is how to insert multiple values into one field for checkbox data.

There are several ways by which we can insert multiple checkbox data into mysql database. In the following section it is discussed.

Way 01 Using Implode function
1)Prepare table to hold multiple checkbox data.
Declare as varchar of the field that will hold checkbox data. Note that your database field must be greater than the summation of all checkbox values length so that it can hold multiple checkbox value.

mysql> create table checkbox(foods varchar(60));
Query OK, 0 rows affected (0.14 sec)

mysql> select * from checkbox;
Empty set (0.00 sec)

2)Note that to insert multiple checkbox values, all checkbox html name must be declared as array. Like in this example it is declared as,
-<input type="checkbox" name="all_food[]" value=$food>$food</input>

And while inserting value into database we need to append a character like comma(,) after each checkbox value so that we can distinguish multiple checkboxes data. This is done by implode function as in described in the example.

<?
if(isset($_POST['submit'])) {
include('include/db.php');
$arrays = implode(",",$_POST['all_food']);
$query="insert into checkbox values('".$arrays."')";
//echo $query;
mysql_query($query) or die ('Error inserting values into database, please go back and try again');
mysql_close();
}
?>

<html>
<body>
<form method="post">
<table width="50%">
<th>What is your favorite food?</th>
<tr>
<?
$array_food=array('fish','meat','vegetable','fastfood','soup');
foreach($array_food as $food){
echo "<td>";
echo "<input type=\"checkbox\" name=\"all_food[]\" id=\"all_food\" value=$food>$food</input>";
echo "</td>";
}

?>

</tr>
<tr>

<td colspan="6" style="padding-left:30em">
<input type="submit" name="submit" value="save">
</td>
</table>
</form>
</body>
</html>

If we run above php code and after check different checkboxes multiple values are inserted into database as below.

mysql> select * from checkbox;
+--------------------------+
| foods |
+--------------------------+
| meat,vegetable,fast,soup |
| meat,vegetable,fastfood |
| fish,meat,fastfood |
+--------------------------+
3 rows in set (0.00 sec)

Way 02: Inserting data using serialize function

<?
if(isset($_POST['submit'])) {
include('include/db.php');
$arrays = serialize($_POST['all_food']);
$query="insert into checkbox values('".$arrays."')";
//echo $query;
mysql_query($query) or die (mysql_error());
mysql_close();
}
?>

<html>
<body>
<form method="post">
<table width="50%">
<th>What is your favorite food?</th>
<tr>
<?
$array_food=array('fish','meat','vegetable','fastfood','soup');
foreach($array_food as $food){
echo "<td>";
echo "<input type=\"checkbox\" name=\"all_food[]\" value=$food>$food</input>";
echo "</td>";
}

?>

</tr>
<tr>

<td colspan="6" style="padding-left:30em">
<input type="submit" name="submit" value="save">
</td>
</table>
</form>
</body>
</html>


From the database it will display,

mysql> select * from checkbox;
+--------------------------------------------------------------+
| foods |
+--------------------------------------------------------------+
| a:3:{i:0;s:4:"meat";i:1;s:9:"vegetable";i:2;s:8:"fastfood";} |
| a:3:{i:0;s:4:"fish";i:1;s:4:"meat";i:2;s:8:"fastfood";} |
| a:2:{i:0;s:4:"meat";i:1;s:4:"soup";} |
+--------------------------------------------------------------+
3 rows in set (0.00 sec)


Here,
- a:3 indicates how many checkbox values are there.
- i:0 indicates it is the first checkbox value inserted. i:1 means it is in the serial second to be inserted data into mysql database and so on.
- s:4 indicates how many letters in the value. s:4 means four letters, s:8 means eight letters.

Related Documents
Datatypes in Php
Integer and floating point number in Php
Variables in PHP

No comments:

Post a Comment