0

I followed the introduction to use prepare to insert some data to the table. The code is like this:

    $wpdb->query(
         $wpdb->prepare(
                "INSERT INTO {$wpdb->prefix}awpcp_adphotos VALUES
                (ad_id, image_name, disabled, isprimary)
                (%d, %s, %d,%d)",
                $ad_id,
                $ad_img,
                0,
                0));

also, disabled and is_primary should be Boolean. But I am not sure what the data type in here.

I also tried

$wpdb->insert(
'{$wpdb->prefix}awpcp_adphotos', // Table name
array(
    'ad_id' => $ad_id,
    'image_name' => $ad_img,
    'disabled' => 0,
    'is_primary' => 0,
), // Columns
array(
    '%s',
    '%d',
    '%d',
    '%d'
) // Explicit formatting

);

But I haven't got any luck yet. There is no error message when I run the code.

Okay, I found answers in somewhere else. it should be like this:

    $wpdb->insert($wpdb->prefix.'awpcp_adphotos', array(
 'ad_id' => $ad_id,
 'image_name' => $image_name,
 'disabled' => $disabled,
 'isprimary' => $isprimary,
  ));

I was confused with prefix format.

  • Do you have debugging information? – s_ha_dum Jun 21 '13 at 18:46
  • Have you looked at $wpdb->last_error to find what the error message is? – Otto Jun 21 '13 at 18:48
  • Guys, come on. You don't need debug to see that these queries are not correct ;) – Krzysiek Dróżdż Jun 21 '13 at 18:53
1

I'm pretty sure that first SQL query isn't correct SQL (insert has different syntax) ;) It should be:

INSERT INTO {$wpdb->prefix}awpcp_adphotos (ad_id, image_name, disabled, isprimary) VALUES (%d, %s, %d,%d)

In second example, I'm almost sure that ad_id is not string, and image_name is not a number. Of cours I'm only guessing, because I haven't seen this table definition.

Remember that you can always turn error reporting for wpdb on. Just put this line in your code and you will see wpdb errors:

$wpdb->show_errors();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.