Monthly Archives: November 2009


Saving Array to MySQL Table

I thought that I would start by making a few posts about some common task, especially in php. One thing that comes up often, id how to dynamically munge and save an array, for instance from $_POST, to the database? The answer is easier than you might think, first, let’s create a munging function, which you can extend for error checking and data sanitization: function munge($array) { if (is_array($array)) { foreach ($array as &$a) { //we keep the key in case we want to do something special here, like verify an email //address or some other field. $a = munge($a); } } else { $array = mysql_real_escape_string($array); $array = “‘$array’”; } return $array; } Munge is an old computer term, sometimes called a backronym: Modify Until Not Guessed Easily, but is usually defined as: iterating over data and altering a piece of data. Now we need to collect and prepare the field names. Now you could just use array_keys, however, It’s better and less bug prone to write your own prep function for the field names. We’ll also implement a whitelist of fields that are allowed to be set into the database. There are many reasons to do this, functional and security wise. I.E. you don’t really know what’s been posted, and it could case an error. $fields_whitelist = array(‘name’,’date’,’message’); function keys_for($array) { global $fields_whitelist; foreach (array_keys($array) as $key) { if (!in_array($key,$fields_whitelist)) { continue; } $keys[] = “`$key`”; } return $keys; } Now that we’ve done that, we need to write something to collect up […]


Editable eBay Item Template 10

This is an editable template, using jQuery, that produces customized versions of an eBay auction item template. I didn’t do the original design, I simply converted it from a table to div based layout, and fixed the spacing and css, then added in the necessary javascript to make it editable. The template hooks into a filemanaging backend in PHP so the templates can be saved and reloaded.