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

    Entering data into DB field with multiple values?

    Hi All,

    I'm trying to work out how I can post some data to my database using a | pipe.

    So for example I have a simple shopping list, and I want the user to select if they have bought the item or not. Yes have a value, no does not.


    Now as you will see each item has a value, so if the first two items were bought only (bread & cheese) in my database i'd post the following values in my database column:


    But how do I do this?

    Normally I only post of Value from one set of radio buttons, so i'd do something like:

    PHP Code:

    if (isset($_POST['Submit']))
    $subsection $_POST['subsection'];
    $result mysql_query("Insert into db (subsection) values ('$subsection')") or die(mysql_error());

    But i'm trying to get different values from lots of radio buttons named with a increment on them, I.e. subsection(i++)and post them in one database column, so how would I do it?

    Is this possible



  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    If these are all from a _POST field, you can use an implode if you're expecting an array. To that extent, create an array of you're radio buttons (ie: name="subsection[0]"... name="subsection[1]"...). This lets you use a foreach and implode on the $_POST['subsection'] item. I would also suggest using a checkbox instead of a radio button, especially since the radio buttons do not have a default value.

    But, and I can't stress this enough, if you're using a delimiter there is something wrong with you're database schema and should be remodeled. A general goal is for BCNF (boyce codd normal form, spelling may be wrong on the names), but the very least you should have is a 3NF level for you're normalization. What you have here is a 2NF format which requires extensive querying to handle update cascade / deletions based on a specified id value (that is, if you delete a product ID of 161, you need to query each record that contains it, extract it, and replace the string). This is called a deletion anomaly, which you should be trying to prevent with practical design techniques.
    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 ;)

  • #3
    New Coder
    Join Date
    Mar 2009
    Chicago, IL
    Thanked 15 Times in 15 Posts
    You can normalize your database structure or you can serialize input array and store it in one field:

    PHP Code:
    $input = array();
    $input[] = 100;
    $input[] = 161;
    $serialized serialize($input); 
    When you retrieve data from database, unserialize() it.

  • #4
    New to the CF scene
    Join Date
    Mar 2009
    Thanked 0 Times in 0 Posts
    Hi stfc_boy
    Is this what you need?
    PHP Code:
    if (isset($_POST['Submit'])){
    $count 0){
    $subsections as $subsection){
    $result mysql_query("Insert into db (subsection) values ('".$values."')") or die(mysql_error());

    <form name="test" action="" method="post">
    <input type="radio" class="checkbox" name="subsection[0]" value="100" /> Yes
    <input type="radio" class="checkbox" name="subsection[0]" value="" /> No
    <input type="radio" class="checkbox" name="subsection[1]" value="161" /> Yes
    <input type="radio" class="checkbox" name="subsection[1]" value="" /> No
    <input type="radio" class="checkbox" name="subsection[2]" value="171" /> Yes
    <input type="radio" class="checkbox" name="subsection[2]" value="" /> No
    <input type="radio" class="checkbox" name="subsection[3]" value="180" /> Yes
    <input type="radio" class="checkbox" name="subsection[3]" value="" /> No
    <input type="radio" class="checkbox" name="subsection[4]" value="308" /> Yes
    <input type="radio" class="checkbox" name="subsection[4]" value="" /> No
    <br />
    <input name="Submit" type="submit" value="Submit" />
    Last edited by bishwadeep; 03-24-2009 at 01:23 PM.


    Posting Permissions

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