I am trying to utilize this function. I cannot figure out why I am still getting the PHP error. All I can figure out is that for some reason the template isn't able to access $wpdb. I don't know what line needs to be placed in the functions.php (in theme folder) file that contains this function. Please help me!!
function submitWeightUpdate( $userid, $weight ) {
global $wpdb;
$result = $wpdb->insert( 'wp_weights', array( 'user_id' => $userid, 'current_weight' => $weight ), array( '%d', '%d' ) );
if (empty($_POST['weight'])) {
$return['error'] = true;
$return['msg'] = 'You did not enter a weight.';
}
else {
$return['error'] = false;
$return['msg'] = 'You\'ve entered: ' . $weight . ' as your new weight.';
}
echo json_encode($return);
}
I also tried setting the theme to the default one and it still won't work, so I have no idea what could be wrong with it!
var_dump( isset( $wpdb ) );
in function and tell what it outputs.$wpdb
be uninitialized in this function, I assume rest of the site is working normally? How exactly you are running this code? In which template/whatever?