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 am working with a custom table where some values will be inserted or updated if they already exists, I have checked and rechecked the syntax and it seems to be working fine, if I run the same query directly to the MySQL server it inserts just fine or updates when the records exist.

My query is this

$query = "INSERT INTO `{$wpdb->base_prefix}apicalls` (`name`, `data`) 
VALUES ('appid','{$appid}')
ON DUPLICATE KEY
UPDATE `data` = '{$appid}';";

Then I have tried

$wpdb->get_results($wpdb->prepare($query));
$wpdb->query($wpdb->prepare($query));

that is not inserting the records, however if I run only the insert statement it inserts fine, I have checked the methods at http://codex.wordpress.org/Class_Reference/wpdb and it does have an insert and also update one, but I could not find one that does both, it does not make sense to me to run two queries when one should suffice.

Am I running the query in a way that wordpress does not support?

Is there a wordpress document that I can read in regards to this?

I have searched but the only two pages I could find were related to posts or if custom queries only to either insert or update, also when doing a google search many people have already asked for custom queries but I couldn't find any with this conditions.

And I was under the assumption that $wpdb->query() should just run the query but for some reason is not going through as if there was a syntax error.

I will keep on testing by modifying the query

Update:

I forgot to add a var_dump($query)

string(107) "INSERT INTO `wp_apicalls` (`name`, `data`) VALUES ('appid','12345') ON DUPLICATE KEY UPDATE data = '12345'";

The database prefix is wp_ and the table exists with that exact name in the database.

share|improve this question
3  
$wpdb->prepare() expects a second argument. Fix that or don’t use that method. Also add debug information to your question. –  toscho Apr 22 '13 at 15:13
    
@toscho I guess I did not read the documentation on that properly, however I had tested with and without it and the outcome was the same. –  Tlacaelel Ramon Luis Apr 22 '13 at 15:20
    
ON DUPLICATE KEY does work when using $wpdb. I use is frequently. There is something wrong with your database keys or with the incorrect use of $wpdb->prepare as toscho points out. –  s_ha_dum Apr 22 '13 at 15:21
    
In your dump data is written without back ticks. Not sure if that’s related. –  toscho Apr 22 '13 at 15:22
    
By the way, no errors are displayed and I have debug defined in the wordpress config file. @ toscho I will check on the data field ticks. –  Tlacaelel Ramon Luis Apr 22 '13 at 15:31

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.