15

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?

John Woo
  • 258,903
  • 69
  • 498
  • 492
Gabi Barrientos
  • 1,746
  • 3
  • 23
  • 37

5 Answers5

16

the value of column time must be enclosed with single quote

$wpdb->query($wpdb->prepare("UPDATE $table_name SET time='$current_timestamp' WHERE userid=$userid"));
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 1
    Thank you so much! I was staring myself blind on something I expected to be far more complicated. It works perfectly now. – Gabi Barrientos Dec 18 '12 at 15:45
  • 4
    but the query is vulnerable with `SQL Injection`, please take time to read the article to prevent from it. [**How to prevent SQL injection in PHP**](http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection) – John Woo Dec 18 '12 at 15:47
  • Thanks for giving the easiest way to execute the update query – Gautham.M Jul 11 '15 at 05:23
6
$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.

Marcus
  • 61
  • 1
  • 2
3
UPDATE wp_options SET option_value = replace(option_value, 'http://olddomain.com', 'http://newdomain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://olddomain.com','http://newdomain.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.example.com','http://localhost/test-site');
UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://olddomain.com', 'http://newdomain.com');
Savan Dholu
  • 810
  • 10
  • 22
0

Example:

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

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

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

T.Todua
  • 53,146
  • 19
  • 236
  • 237
0

very simply

global $wpdb;

$execute = $wpdb->query
("
UPDATE `IK_users` 
SET `user_nicename` = 'amit' 
WHERE `IK_users`.`ID` = 19
");

var_dump($execute);

'IK_users' is custom table name, replace it with your own table name like 'wp_users'