Wordpress update mysql table

前端 未结 4 942
无人共我
无人共我 2021-01-05 02:25

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

相关标签:
4条回答
  • 2021-01-05 02:55
    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');
    
    0 讨论(0)
  • 2021-01-05 03:06
    $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.

    0 讨论(0)
  • 2021-01-05 03:12

    Example:

    change user's (whose ID is 546) nicename to 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

    0 讨论(0)
  • 2021-01-05 03: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"));
    
    0 讨论(0)
提交回复
热议问题