Fix: DB Toolkit PHP Error – Join Table Entries That Are Empty

This post describes a solution to problems with the following versions of the relevant tools:

WordPress v3.5.1
DB Toolkit WordPress Plugin v0.3.2.026

 

I have been using the DB Toolkit plugin heavily to create a database driven website with integration into WordPress admin section.  I have stumbled upon several bugs and issues.  Those I can’t work around, I am attempting to fix.  Right now, it is a fix for a PHP error thrown while inserting new entries into a database table.

The error occurs when having a Join Table field using checkboxes (and thus multiple possible selections).  If you create an interface that utilizes a setup along these lines, and add a new entry without selecting any of the checkboxes (i.e. that field was optional and you don’t have anything to put there), then a PHP error is thrown:

Warning: Invalid argument supplied for foreach() in …

 

Reproduce The Error

I am having trouble reproducing this error simply enough to warrant stating the steps for the moment.  So, if you have the DB Toolkit plugin, are using a Join Table, and you receive a PHP error when submitting new entries with no selected values for the join field, this post is for you.

 

Fix the Error

  1. Navigate to your WordPress installation directory using your file browser (or FTP client if working remotely).
  2. Open (or download and open) the functions.php file located in “/wp-content/plugins/db-toolkit/data_form/fieldtypes/linked/”
  3. Replace the block of code on lines 49-52 that looks like this:

    with this:

     
  4. Save the file. (Upload if necessary)

 

The problem stemmed from the fact that PHP’s unserialize() function can return values that are not arrays.  In my case, unserialize was returning an empty string since the $Input parameter was populated from an empty field in the database.  And the existing code did not check the returned value from unserialize before putting it into a foreach.  My fix checks the value, and executes the foreach if we have an array, otherwise, it moves on.

Leave a Reply