SlickRemix
SlickRemix

Reputation: 466

Missing argument 2 for wpdb::prepare()

How can I get this to work without getting the this warning in my error log. Missing argument 2 for wpdb::prepare()

I know I need to pass the second argument as noted in wp docs but having trouble getting it to work properly.

        $insert = "INSERT IGNORE INTO " . $pps_new_table_name . "( post_id, post_author, create_date, hit_count, {$browser_column}, {$device_column} ) VALUES (" . $_POST['post_id'] . ",'" . $_POST['post_author'] . "','" . $create_date . "','1','1','1') ON DUPLICATE KEY UPDATE hit_count=hit_count + 1, {$browser_column}= {$browser_column} + 1, {$device_column}= {$device_column} + 1";
        $results = $wpdb->query($wpdb->prepare($insert));

Upvotes: 1

Views: 237

Answers (1)

SlickRemix
SlickRemix

Reputation: 466

I needed to pass the variables in the VALUES area...

VALUES (%s, %s, %s,'1','1','1')

and add the array right after my $insert string moving my original VALUE strings below.

$results = $wpdb->query($wpdb->prepare($insert, array(
                                    $_POST['post_id'], 
                                    $_POST['post_author'], 
                                    $create_date
                                )));

So in total it looks like this.

$insert = "
            INSERT IGNORE INTO " . $pps_new_table_name . "( post_id, post_author, create_date, hit_count, {$browser_column}, {$device_column} ) 
            VALUES (%s, %s, %s,'1','1','1') 
            ON DUPLICATE KEY UPDATE hit_count=hit_count + 1, {$browser_column}= {$browser_column} + 1, {$device_column}= {$device_column} + 1";
            $results = $wpdb->query($wpdb->prepare($insert, array(
                                    $_POST['post_id'], 
                                    $_POST['post_author'], 
                                    $create_date
                                )));
            $wpdb->show_errors();

Upvotes: 2

Related Questions