Wordpress 更新 mysql 表

2022-01-17 00:00:00 sql-update sql mysql wordpress

我正在为 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.

我使用的代码如下:

$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列的值必须用单引号

$wpdb->query($wpdb->prepare("UPDATE $table_name SET time='$current_timestamp' WHERE userid=$userid"));

相关文章