www.webdeveloper.com
Results 1 to 5 of 5

Thread: Array to Database? Should I use serialize?

  1. #1
    Join Date
    Sep 2010
    Posts
    30

    Cool Array to Database? Should I use serialize?

    Hi all,

    I have small expanding script that allows for up to 3 additional input fields:

    The form portion:


    <div id="dynamicInput">Uniform Resource Locator 1:<br><input type="text" name="myInputs[]" id="myInputs[]" placeholder="http://www.mydomainname.com">
    </div><br/>
    <input type="button" class="add-more" value="Add Additional URL's / Domains" onClick="addInput('dynamicInput');">

    The script portion:

    $(".add-more").click(function(){
    var new_field = '<textarea name="myInputs[]"></textarea>';
    $(this).before(new_field);

    });

    The database portion:

    $sql_insert = "INSERT into `users`
    (`full_name`,`user_email`,`pwd`,`address`,`tel`,`fax`,`website`,`date`,`users_ip`,`activation_code`, `country`,`user_name`,`webone`,`webtwo`,`webthree`)
    VALUES
    ('$data[full_name]','$usr_email','$sha1pass','$data[address]','$data[tel]','$data[fax]','$data[web]'
    ,now(),'$user_ip','$activ_code','$data[country]','$user_name','$myInputsA','$myInputsB','$myInputsC'
    )
    ";

    Im trying to work with an array of inputs and add the inputs to my users table. But I'm not sure if I should should use serialize for this or work with the "myInputs" array. How would I work with the array in this fashion?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    I would not serialize() the inputs, as that makes it very difficult to do anything with it from a database standpoint, other than retrieving it so that you can use the array within your application code. The precise mechanics of how I'd put it in the query would depend in part on which database extension you are using, e.g. whether you can use prepared statements as with the PDO or MySQLi extensions, or using mysql_real_escape_string() with the older (and deprecated) MySQL extension, and so forth.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2010
    Posts
    227
    I am a big user of serializing to store in MySQL.

    However I have learned that it is best really for fixed data, say backing up an exported excel spreadsheet generated by a website.

    For small arrays, I far and away prefer to store it as a string.
    This is because it allows me to still use built in sql functions
    It also allows me to make quick changes or searchs from something like phpmyadmin

    So my friend, I would suggest that you look heavily at explode and implode to store arrays, including multidimensional and associative arrays.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    Or you could look into normalizing your data instead of cramming multiple data elements into a single data field. To be "1st normal form" compliant, those multiple, similar inputs should be in a separate table, each in a separate row with a column that points to the primary key of the "users" table, something like:
    Code:
    user_link
    ==========
    user_id (or whatever the primary key column is for users?)
    link (probably a varchar field?)
    Now you can pull them in with the user data whenever you want them with a JOIN or LEFT JOIN in your SQL, and if you ever decide to change the number of links a user can submit, no changes are needed to the database, just to your application logic.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    851
    Normalize! Normalize! Normalize!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles