Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2010, 02:08 PM   PM User | #1
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
How do you take multiple values from Checkboxs

I have this form

PHP Code:
<?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..

PHP Code:
$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?
kevinkhan is offline   Reply With Quote
Old 01-25-2010, 02:31 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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:
PHP Code:
$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:
Code:
+---------------+            +------------------+
|    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.

Edit:
BTW, are you aware that all of you're checkboxes will have the same value?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 01-25-2010 at 02:33 PM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
kevinkhan (01-25-2010)
Old 01-25-2010, 02:32 PM   PM User | #3
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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;
PHP Code:
$events $_POST['events']; 

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

              
        
    
$result mysql_query($query$connection);  

angst is offline   Reply With Quote
Old 01-25-2010, 02:35 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by angst View Post
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;
PHP Code:
$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
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-25-2010, 02:37 PM   PM User | #5
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
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.
angst is offline   Reply With Quote
Old 01-25-2010, 02:42 PM   PM User | #6
kevinkhan
Regular Coder

 
Join Date: Jun 2009
Posts: 350
Thanks: 75
Thanked 0 Times in 0 Posts
kevinkhan is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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:
PHP Code:
$events implode(', '$_POST['events']); 
Thanks this exactly what i wanted
kevinkhan is offline   Reply With Quote
Old 01-25-2010, 02:44 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by kevinkhan View Post
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:51 AM.


Advertisement
Log in to turn off these ads.