Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Update row in custom wordpress table

Ive Tried And Tried Looking Up How To Approach Editing/Updating Rows In Custom Wordpress Tables I Have It Set To Grab Current logged in user's username which then compares to a custom wp_ table With UserNAme As The Primary Key Which Then I Would Like To Edit A Particular Column/Field On That Particular Row By Passing The Value Of A Variable Over After It Confirms Current Logged In User Matches The Primary Key UserName In My Custom Table"wp_customers" What Am I Doing Wrong With This Line Of Code Or Do You Have A Better Solution

    $current_user = wp_get_current_user();

$johnny = $current_user->user_login;
$subs = 'illinois';
global $wpdb;
$wpdb->query(
    "
    UPDATE $wpdb->wp_Customers 
    SET BuyersAddress = $subs
    WHERE UserName = $johnny

    ");
like image 431
Charles Houser Junior Avatar asked Jun 17 '17 03:06

Charles Houser Junior


2 Answers

Try this code

A simple WordPress Update query

WP Update

$current_user = wp_get_current_user();

$johnny = array('UserName' => $current_user->user_login);
$subs = array('BuyersAddress' => 'illinois');
global $wpdb;
$table_name  = $wpdb->prefix."Customers";

$wpdb->update($table_name, $subs, $johnny);

Hope this will help you

like image 199
deemi-D-nadeem Avatar answered Sep 28 '22 20:09

deemi-D-nadeem


Try this code.

$current_user = wp_get_current_user();

    $johnny = $current_user->user_login;
    $subs = 'illinois';
    global $wpdb;
    $table_name  = $wpdb->prefix."Customers";

    $wpdb->query( $wpdb->prepare("UPDATE $table_name 
                SET BuyersAddress = %s 
             WHERE UserName = %s",$subs, $johnny)
    );
like image 34
Vel Avatar answered Sep 28 '22 18:09

Vel