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 Answers
Reset to default 0Please 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
wp_get_current_user()
shouldn't have a$
at the beginning. – Jacob Peattie Commented May 5, 2020 at 3:19if()
statement uses=
(an assignment operator) instead of==
or===
(comparison operators). This can have unintended consequences. – Pat J Commented May 5, 2020 at 3:22