Inserting data from multiple selections of checkbox into db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
patelh5
Forum Newbie
Posts: 3
Joined: Wed Sep 24, 2008 12:06 pm

Inserting data from multiple selections of checkbox into db

Post by patelh5 »

Hi,

Can someone explain to me step by step and clearly, how I can insert data from multiple selections made on a checkbox into a column name in a database in MySQL. I understand how to insert the data, but with several different options this can become hard.

Code: Select all

Choose the Type of Issue
<input type="checkbox" name="general" value="value1">General
<input type="checkbox" name="server" value="value2">Server
one value can be automatically stored into the fieldname of the database, but when you have several options to pick from, or even if one is picked from a range it becomes hard to store these values in the db, I guess this is where Arrays come in, if someone can explain further as I am new to PHP.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Inserting data from multiple selections of checkbox into db

Post by papa »

Read about arrays and the very helpful serialize method. With that you can store a lot of values in one db table column for example.
mistymorningglory
Forum Newbie
Posts: 5
Joined: Fri Sep 26, 2008 4:27 pm

Re: Inserting data from multiple selections of checkbox into db

Post by mistymorningglory »

Okay, here's how you go about it...
All of your checkboxes should have the same name with [] after it... i.e.

Code: Select all

 
<input type='checkbox' name='something_cool[]' value="apples" >Apples <br>
<input type='checkbox' name='something_cool[]' value="oranges" >Oranges <br>
<input type='checkbox' name='something_cool[]' value="pears" >Pears <br>
 
When the user clicks submit, the values they selected will be passed to php as an array.
in this case the array would be referenced as $_POST['something_cool']

If the user doesn't select any of the checkboxes, $_POST['something_cool'] is null or non-existent, don't forget to check for this or it will throw errors!

If they select just apples, the value of $_POST['something_cool'] would be this:
$_POST['something_cool']=>Array('apples');

Whereas if they selected multiple values it would look like this:
$_POST['something_cool']=>Array('apples','oranges','pears');

To what exactly your forum is sending to php, you can use print_r at the top of your form processing page.

Code: Select all

 
echo('<pre>');
print_r($_POST);
echo('<pre>');
die();
 
It may help you to visualize what's happening.

Hopefully that makes sense, if not stop here and spend a fair amount of time reading about arrays.

Okay, so now you know that you can have an array of values assigned to $_POST['something_cool'].
Now you want to stick it in to one field in your database.

Let me pause here and say that in my opinion, arrays in databases are sloppy. I would tend to create a linked table to record this info rather than store it as an array in one filed. That is up for debate, other coders will say there's no problem with it.

To store the data in the database you could use something like the following:

Code: Select all

 
if(is_array($_POST['something_cool'])){
    $insert_value = serialize($_POST['something_cool']);
    $sql = "UPDATE my_table SET `cool_column` = ".$insert_value." WHERE id = ".$_POST['id']; // INSERT YOUR OWN DARN SQL HERE, this is just an example
}
 
What that is going to do is stick something in your database that looks pretty darn goofy.
It takes the php array and turns it in to a long text string that the database will not choke on.

Assuming you actually want to display the info that was stored, you would need to "unserialize" it.
That will turn it back in to a php array instead of a long goofy line of text

for example you use something like this to get it out of the database:

Code: Select all

 
$sql = "SELECT * FROM my_table";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
    $usable_array = unserialize($row['cool_column'];
    foreach($usable_array as $key => $value){
        echo($value);
    }
}
 
Hope this helps.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Inserting data from multiple selections of checkbox into db

Post by VladSun »

:)
I would never use a serialized object to store data in DB :)
It's too much inflexible and too hard to maintenance.
Maybe a bit mask is more appropriate in this case.
There are 10 types of people in this world, those who understand binary and those who don't
patelh5
Forum Newbie
Posts: 3
Joined: Wed Sep 24, 2008 12:06 pm

Re: Inserting data from multiple selections of checkbox into db

Post by patelh5 »

Ah thanks for that, but does it go into the database serialized and then taken out serialized? I could still use what you got there and try it out which I shall.

At the moment, I have just added the values into the database using this code, similar to what you said before:

$issue=$_POST['issue'];
$tempvar = '';

$tempvar = $issue[0] . ' ' . $issue[1];

The last statement separates the values stored in the database, with an empty space. But when taking this out the database, that is the question??? or would it be just as simple as Select, from?
patelh5
Forum Newbie
Posts: 3
Joined: Wed Sep 24, 2008 12:06 pm

Re: Inserting data from multiple selections of checkbox into db

Post by patelh5 »

Also, a quick explanation of this:-

Code: Select all

if(is_array($_POST['something_cool'])){
    $insert_value = serialize($_POST['something_cool']);
    $sql = "UPDATE my_table SET `cool_column` = ".$insert_value." WHERE id = ".$_POST['id']; // INSERT YOUR OWN DARN SQL HERE, this is just an example
what is $insert_value, do you mean insert_apples or something? and also what is id = ".$_POST['id']; representing at the moment?

And when you mean insert yor own SQL here, do you mean the normal insertion of data into the SQL database like this:

Code: Select all

$sql="INSERT INTO $tbl_name(Issue, Priority, Type)VALUES('$comments', '$priority', '$tempvar')";
$result=mysql_query($sql);
Post Reply