PHP Implode Issue

Discussion in 'PHP' started by alan.wittes, Jul 2, 2007.

  1. #1
    I am using PHP to take data from an HTML input form to a MYSQL database. I have an issue with one of my fields. The field is 'Language'.

    Here is what I am after: I need to have multiple 'Language' checkboxes, so that the user can check all the Languages they speak (could often be more than 1). For example, let's say the user selects 'Spanish' and 'Italian'. I would then like to store this value in my MYSQL database as 'Spanish, Italian'.

    Below is my code as it stands today:

    HTML Code:
    <input name="AG_LANGUAGE[]" type="checkbox" value="Spanish" /> Spanish
    <input name="AG_LANGUAGE[]" type="checkbox" value="Italian" /> Italian

    PHP Code:
    <?php
    $language = (!empty($_POST['AG_LANGUAGE'])? implode(',',$_POST['AG_LANGUAGE'])."\n" : '');
    ?>

    The problem I'm having is that the input data is being stored in my Language table field as 'Array'. Is there a way to store the actual values in the field instead (ex. 'Spanish, Italian')? If not, how do I extract the actual values using SQL? Writing a simple SQL query produces the below result:

    select ag_language
    from ag_profile

    'Array'

    Thanks in advance for any help.
     
    alan.wittes, Jul 2, 2007 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    You can take a look at serialize - but really, you can't save an array ... just a string version of the array that you can explode or unserialize later.

    don't forget to do some security checking on that data input before you use it. While your users may be clicking check boxes they can spoof forms and give malicious results.
     
    sarahk, Jul 2, 2007 IP
    Katy likes this.
  3. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #3
    insert mysql_real_escape_string(serialize($your_array)) into the field

    deserialize(stripslashes($the_table_row))
     
    KalvinB, Jul 2, 2007 IP