Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm writing a plugin called 'Featured Releases' for a client. The user fills out a short form in the admin area which inserts some text and an image id into the wordpress database via $wpdb->insert. On my development machine it works fine, but on the production server (which has over 500 posts of all sorts, mostly images), the value of the image id is being changed to 127 no matter what I do. 127 is the id of a post, but its not an image and certainly not the id that was passed from the form.

The image is uploaded via the native wordpress media uploader. The image's post id (from the wp_posts table) is passed to a hidden input in the form. On submit, this function handles the $_POST data:

function fr_add_album() {
    global $wpdb;

    if ( isset($_POST) && (count($_POST) > 0) ) {

        check_admin_referer('featured-releases-add_album');

        $table_name = $wpdb->prefix . 'featured_releases';

        $desired_keys = array('fr_band', 'fr_album', 'fr_album_description', 'fr_image_id', 'fr_shop_url');

        $data = array();
        foreach( $desired_keys as $key ) {

            // Define and sanatize data
            if ( isset($_POST["$key"]) ) {
                $accepted_html = array('a' => array('href' => array(), 
                                       'title' => array())); 
                $data["$key"] = wp_kses($_POST["$key"], $accepted_html);

                unset($_POST["$key"]);
            } else {
                $data["$key"] = '';
            }
        }

        // Sanitize the shop url
        if ( isset($data['fr_shop_url']) ) {
            $data['fr_shop_url'] = esc_url($data['fr_shop_url']);
        }

        $data['fr_modified'] = date('Y-m-d h:i:sa');

        error_log($data['fr_image_id']); // This returns the correct value!

        $wpdb->insert($table_name, $data, array('%s','%s', '%s', '%d', '%s'));

        // After the insert, the fr_image_id column always reads "127"

    }
}

The insane thing is that there are a few images which do post correctly. These were the first images I uploaded when I created the wordpress install. Every other image from any other date, including any new image I upload is saved as 127 in the database. All the images have the same owners and same permissions. I also can't see any difference in wp_posts between the new images and the old. Any ideas? I'm dyin' here. Thanks for your help!

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Great advice Rarst! The output of $wpdb->queries was exactly what it should be:

"INSERT INTO `wp_featured_releases` (`fr_band`,`fr_album`,`fr_album_description`,`fr_image_id`,`fr_shop_url`,`fr_modified`) VALUES ('','','','504','','2011-01-14 10:49:58pm')"

504 is the correct id for the image, but the value in the table is still 127.

My latest thought is that my mysql skills have failed me. Images with an ID below 127 work fine, but anything above 127 is changed to 127. The column type that holds fr_image_id is tinyint(9), which should be able to hold any value up to 9999999999 right? If not, I think I just found my answer. I'll try changing it and see.

share|improve this answer
    
Holy crap that's it! Maximum value for tinyint columns is 127! –  likesalmon Jan 14 '11 at 23:05
    
Since I wrote the question before registering, I can't figure out how to mark this as solved! –  likesalmon Jan 14 '11 at 23:13
    
when clicking accept checkmark you should be getting message saying you need to wait some time to accept answer to your own question. If you are not getting option to accept answer at all it seems like a bug to report on meta. –  Rarst Jan 15 '11 at 13:34
    
@Rarst That must have been it. I wasn't seeing a checkmark yesterday but today it was there. –  likesalmon Jan 16 '11 at 0:24
add comment

Cryptic indeed. Something goes wrong only in production and only on brief stage of query execution (which is used extensively and theoretically quite reliable)...

Had you tried to check generated SQL query for sanity? ( Save queries for analysis ). I'd first try that to see what is really sent to database and if it seems fine if same query runs ok when performed manually with PHPMyAdmin or other tool.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.