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.
$wpdb->prepare()
expects a second argument. Fix that or don’t use that method. Also add debug information to your question. – toscho♦ Apr 22 at 15:13ON 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 at 15:21data
is written without back ticks. Not sure if that’s related. – toscho♦ Apr 22 at 15:22