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.