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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    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.

    http://www.inspireaway.co.uk/test3.php

    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:

    100|161|

    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

    Thanks

    Chris

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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'); 

  • #3
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    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
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi stfc_boy
    Is this what you need?
    PHP Code:
    if (isset($_POST['Submit'])){
        
    $subsections=array();
        
    $subsections=$_POST['subsection'];
        
    $count=count($subsections);
        if(
    $count 0){
            
    $delim="";
            foreach(
    $subsections as $subsection){
                if(
    $subsection){
                    
    $values.=$delim.$subsection;
                    
    $delim="|";
                }
            }
        }
        
    $result mysql_query("Insert into db (subsection) values ('".$values."')") or die(mysql_error());

    <form name="test" action="" method="post">
    <h3>Foods</h3>
    <p><b>Bread</b></p>
    <input type="radio" class="checkbox" name="subsection[0]" value="100" /> Yes
    <input type="radio" class="checkbox" name="subsection[0]" value="" /> No
    <p><b>Cheese</b></p>
    <input type="radio" class="checkbox" name="subsection[1]" value="161" /> Yes
    <input type="radio" class="checkbox" name="subsection[1]" value="" /> No
    <p><b>Crackers</b></p>
    <input type="radio" class="checkbox" name="subsection[2]" value="171" /> Yes
    <input type="radio" class="checkbox" name="subsection[2]" value="" /> No
    <p><b>Ham</b></p>
    <input type="radio" class="checkbox" name="subsection[3]" value="180" /> Yes
    <input type="radio" class="checkbox" name="subsection[3]" value="" /> No
    <p><b>Beef</b></p>
    <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" />
    </form>
    Last edited by bishwadeep; 03-24-2009 at 12: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
    •