Wordpress 更新 mysql 表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13936448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:49:11  来源:igfitidea点击:

Wordpress update mysql table

mysqlsqlwordpresssql-update

提问by Gabi Barrientos

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.

我正在为 Wordpress 编写一个插件,它应该检查是否已经存在一个 mysql 条目。
如果它不存在 Wordpress 应将条目插入表中。这部分按我的预期工作。
但是,如果 mysql 表中已经有一个条目,Wordpress 应该更新该表,但这不起作用。

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?

有人可以告诉我我在这里做错了什么吗?
解决这个问题的正确方法是什么?

回答by John Woo

the value of column timemust be enclosed with single quote

列的值time必须用single quote

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

回答by Marcus

$result = $wpdb->update('westend_areaofficers', array('officerOrder' => $memberOrder,
'officerTitle' => $memberTitle, 'officerName' => $memberName, 'officerPhone' => 
 $memberPhone), array('officerId' => $memberId), array('%d','%s', '%s', '%s'),
 array('%d'));

if($result > 0){
echo "Successfully Updated";
}
else{
  exit( var_dump( $wpdb->last_query ) );
}
$wpdb->flush();

The above solution is what worked for me because using the $wpbd->query($wpbd->prepare()) statement didn't work even when passing in the correct number and string formats.
The purpose of the var_dump() function is to see where the execution of the query went wrong. It prints out the query and values being passed. Of course using the $wpdb->flush() function clears the cache for the next query to execute.

上面的解决方案对我有用,因为即使传入正确的数字和字符串格式,使用 $wpbd->query($wpbd->prepare()) 语句也不起作用。
var_dump() 函数的目的是查看查询执行出错的地方。它打印出正在传递的查询和值。当然,使用 $wpdb->flush() 函数会清除缓存以便执行下一个查询。

回答by Savan Dholu

UPDATE wp_options SET option_value = replace(option_value, 'http://www.example.com', 'http://localhost/test-site') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.example.com', 'http://localhost/test-site');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.example.com','http://localhost/test-site');

回答by T.Todua

Example:

例子:

change user's (whose ID is 546) nicename to Harde_Bande

将用户(其 ID 为 546)的昵称更改为Harde_Bande

$execut= $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->users SET user_nicename = %d WHERE ID = %s", "Harde_Bande", 546 ) );
var_dump($execut);

Learn more at: http://codex.wordpress.org/Class_Reference/wpdb#Examples

了解更多信息:http: //codex.wordpress.org/Class_Reference/wpdb#Examples