...

View Full Version : How do you take multiple values from Checkboxs



kevinkhan
01-25-2010, 03:08 PM
I have this form


<?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?>
<?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?>
<?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?>
<?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?>
<?php echo "<input class=\"events\" type=\"checkbox\" name=\"events[]\" value=\"Teenage Discos\" /><span>Teenage Discos</span><br />";?>

and trying to extract the values out and insert them into a database..


$events = $_POST['events'];

$query = "INSERT INTO user
(
events
)
VALUES
(
'{$events}'
)";


$result = mysql_query($query, $connection);



When i go to the database it shows up as array.. How do i get it to show up as the values?

Fou-Lu
01-25-2010, 03:31 PM
Depends on what you want to do with you're data. the $_POST['events'] itself is an array, which is why it shows up as 'array'. You can comma separate it with:


$events = implode(', ', $_POST['events']);


You can look at normalizing the database as well. It appears you're combining a many-to-one relationship against a single table.
Such a design is more suited for something along this lines:


+---------------+ +------------------+
| User | | UserEvents |
+---------------+ +------------------+
| userID [PK] |+----------<| userID [PK][FK] |
| userName [UK] | | event [PK] |
| ... | | ... |
+---------------+ +------------------+


This lets you store multiple events entry for each user associated without needing to concern yourself with anomolies.


BTW, are you aware that all of you're checkboxes will have the same value?

angst
01-25-2010, 03:32 PM
the "[]" on the end of the element name makes it an Array, so $_POST wont give you all the results,
you must loop through the data and update as needed.

try this;


$events = $_POST['events'];

foreach($events As $event){
$query = "INSERT INTO user
(
events
)
VALUES
(
'{$event}'
)";


$result = mysql_query($query, $connection);
}

Fou-Lu
01-25-2010, 03:35 PM
the "[]" on the end of the element name makes it an Array, so $_POST wont give you all the results,
you must loop through the data and update as needed.

try this;


$events = $_POST['events'];

foreach($events As $event){
$query = "INSERT INTO user
(
events
)
VALUES
(
'{$event}'
)";


$result = mysql_query($query, $connection);
}



o.O
We saw this as two completely different things! You saw it as multiple entries while I was under the impression of a single entry with multiple values! Now we gotta wait for the OP to get back >.<
Lol

angst
01-25-2010, 03:37 PM
hehe, yah i was just thinking the same thing after I posted. though looking at the table now I think that you might be right.

kevinkhan
01-25-2010, 03:42 PM
Depends on what you want to do with you're data. the $_POST['events'] itself is an array, which is why it shows up as 'array'. You can comma separate it with:


$events = implode(', ', $_POST['events']);




Thanks this exactly what i wanted ;)

Fou-Lu
01-25-2010, 03:44 PM
Thanks this exactly what i wanted ;)

Ok. Consider the normalization aspect of you're data as well. Its important to have the proper database / storage design before you start implementing code. It becomes more difficult as time goes by to later normalize you're data.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum