Wordpress 更新 mysql 表 [英] Wordpress update mysql table

查看:37
本文介绍了Wordpress 更新 mysql 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 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"));

这篇关于Wordpress 更新 mysql 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆