WordPress更新MySQL表 [英] Wordpress update mysql table
问题描述
我正在为Wordpress编写一个插件,该插件应检查mysql条目是否已存在.
如果不存在,Wordpress应该将该条目插入表中.这部分符合我的预期.
但是,如果mysql表中已经有一个条目,则Wordpress应该更新该表,但这不起作用.
I am writing a plugin for Wordpress, which should check if a mysql entry already exists.
If it does not exist Wordpress should insert the entry into the table. This part works as I expected.
However, if there already is an entry in the mysql table, Wordpress should update the table, but this does not work.
我为此使用的代码是以下代码:
The code I am using for this is the following code:
$wpdb->query($wpdb->prepare("UPDATE $table_name SET time=$current_timestamp WHERE userid=$userid"));
我在此查询中使用的变量是正确的,因为它们已成功用于插入表中,但是在更新过程中出现了问题.
The variables I use in this query are correct, because they are successfully used to insert into the table, but somewhere along the way something goes wrong with the updating function.
有人可以告诉我我在做什么错吗?
解决这个问题的正确方法是什么?
Can somebody please tell me what I am doing wrong here?
What is the right way to go about this?
推荐答案
列time
的值必须用single quote
$wpdb->query($wpdb->prepare("UPDATE $table_name SET time='$current_timestamp' WHERE userid=$userid"));
这篇关于WordPress更新MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!