Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top