Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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?
    Last edited by Fou-Lu; 01-25-2010 at 02:33 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    kevinkhan (01-25-2010)

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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);  


  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    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

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •