为什么全局变量的值在插入数据库时返回空

时间:2020-05-05 作者:pandglobal

我在使用全局变量将值插入自定义数据库表时遇到问题:

// 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. 
在进行数据库查询时,这些值在代码中工作正常,但一旦插入数据库,它将返回一个空值。

    // 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,
        )); 
    }

2 个回复
SO网友:Sanjay Gupta

请尝试使用更新的代码

// 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,
    )); 
}

SO网友:pandglobal

谢谢大家,@Pat J you指出了真正的问题,我应该使用if($age==19)这个比较运算符==修复了问题,并更新了上面的代码