最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Why is values of a global variable returning blank when inserted into database

programmeradmin0浏览0评论

I am having an issue with using global variables to get values inserted into my custom database table:

// example that does not work 
$user = wp_get_current_user();
$mega = $user->user_login;   // this values will return blank when
                             // inserted into database especially 
                             // when used with an if statement. 

The values work fine within the code in making a database query, but once inserted into the database it returns a blank empty value.

    // here is my full code
    global $wp;
    $user = wp_get_current_user();
    $mega = $user->user_login;
    $age = 19;

    // if records were not found in the database 
    // then insert new row with the details
    if ( $age == 19) {
        $fillup = $wpdb->insert("markrecord_table", array(
            "username" => $mega,
            "post_id" => 340,
            "counted" => 1,
        )); 
    }

I am having an issue with using global variables to get values inserted into my custom database table:

// example that does not work 
$user = wp_get_current_user();
$mega = $user->user_login;   // this values will return blank when
                             // inserted into database especially 
                             // when used with an if statement. 

The values work fine within the code in making a database query, but once inserted into the database it returns a blank empty value.

    // here is my full code
    global $wp;
    $user = wp_get_current_user();
    $mega = $user->user_login;
    $age = 19;

    // if records were not found in the database 
    // then insert new row with the details
    if ( $age == 19) {
        $fillup = $wpdb->insert("markrecord_table", array(
            "username" => $mega,
            "post_id" => 340,
            "counted" => 1,
        )); 
    }
Share Improve this question edited May 6, 2020 at 1:51 pandglobal asked May 5, 2020 at 3:11 pandglobalpandglobal 431 silver badge9 bronze badges 2
  • 2 wp_get_current_user() shouldn't have a $ at the beginning. – Jacob Peattie Commented May 5, 2020 at 3:19
  • 1 Your if() statement uses = (an assignment operator) instead of == or === (comparison operators). This can have unintended consequences. – Pat J Commented May 5, 2020 at 3:22
Add a comment  | 

2 Answers 2

Reset to default 0

Please try with the updated code

// here is my full code
global $wpdb;
$user = wp_get_current_user();
$mega = $user->user_login;
$age = 19;

// if records were not found in the database 
// then insert new row with the details
if ( $age == 19) {
    $fillup = $wpdb->insert("markrecord_table", array(
        "username" => $mega,
        "post_id" => 340,
        "counted" => 1,
    )); 
}

Thanks everyone, @Pat J you pointed out the real issue, i shoud have used if ($age == 19) that comparison operator == fixed the issue and have updated my code above

发布评论

评论列表(0)

  1. 暂无评论