WordPress database query and get_user_meta return incorrect data

Hi guys.

I have a PHP script that gets details from a checkbox and a text field. The console.log data is correct and so is the updated data in the database, however, when I try and get the data, using $wpdb or get_user_meta(), I receive incorrect data (i receive previously updated data, even though in the database te data is different).

Here’s my code:

//Form

<form id="donations" method="POST" >
  <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php
      $option = get_the_title();
      $id = get_the_ID();
    ?>
    <input type="radio" class="charity" id="charity-<?php echo $id ?>" name="charity" value="<?php echo $id ?>" <?php if ($_POST[$id] == $id) echo 'checked'; ?> >
    <label for="<?php echo $id ?>"><?php echo $option; ?></label>
    <br>
  <?php endwhile; ?>
  <?php wp_reset_query(); ?>
  <div class="more-don--wrapper">
    <label for="more_donation">The standard card cost is R50.00. <br>Enter a different amount below if you would like to donate more (must be more than R50.00).</label>
    <input type="number" min="60" step="10" placeholder="50.00" id="more_donation" name="more_donation" value="<?php if (isset($_POST['more-donation'])){ echo $_POST['more-donation']; }else{ echo '50'; } ?>"/>
    <span class="more_donation_tc">All additional amount will go directly to the nominated charity.</span>
  </div>
  <!--<input class="button" type="submit" value="Update Selection" />-->
</form>

//Ajax script

jQuery( 'document' ).ready( function( $ ) {

  // Form submission listener
  //$( '#donations' ).submit( function() {
  $('.choose-charity').click(function() {

      // Grab our post meta value
      var charity = $( '#donations .charity:checked' ).val();
      var more_donation = $( '#donations #more_donation' ).val();

      // Do very simple value validation
      //if( $( '#donations #charity #donation' ).val().length ) {

          $.ajax( {
            url : ajax_url,                   // Use our localized variable that holds the AJAX URL
            type: 'POST',                     // Declare our ajax submission method ( GET or POST )
            data: {                           // This is our data object
                action: 'um_donation',        // AJAX POST Action
                'charity': charity,           // Replace `um_key` with your user_meta key name
                'more_donation': more_donation,
            }
          })
          .success( function( results ) {
            console.log( 'User Meta Updated! ' + charity + ' ' + more_donation );
          })
          .fail( function( data ) {
            console.log( data.responseText );
            console.log( 'Request failed: ' + data.statusText );
          });

      //} else {
          // Show user error message.
      //}

      return false;   // Stop our form from submitting

  });

});

// Data update script

function donations_callback() {

	// Ensure we have the data we need to continue
	if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {

			// If we don't - return custom error message and exit
			header( 'HTTP/1.1 400 Empty POST Values' );
			echo 'Could Not Verify POST Values.';
			exit;
			
	}

	$user_id        = get_current_user_id();                            // Get our current user ID
	$charity        = sanitize_text_field( $_POST['charity'] );      		// Sanitize our user meta value
	$more_donation  = sanitize_text_field( $_POST['more_donation'] );   // Sanitize our user email field

	if( !empty($charity) ) {
		update_user_meta( $user_id, 'charity', $charity );                	// Update our user meta
	}else {
		update_user_meta( $user_id, 'charity', '' );    
	}

	if( !empty($more_donation) ) {
		update_user_meta( $user_id, 'more_donation', $more_donation ); 			// Update our user meta
	} else {
		update_user_meta( $user_id, 'more_donation', '' );
	}

	//update_field('charity', $charity, 'user_'.$user_id.'');
	//update_field('more_donation', $more_donation, 'user_'.$user_id.'');

	exit;

}
add_action( 'wp_ajax_nopriv_um_donation', 'donations_callback' );
add_action( 'wp_ajax_um_donation', 'donations_callback' );

And this is my loop in a tabular form. The first tab is where the form (above) is and the last tab is where I retrieve the data and the code is as follows:

<?php
  // get saves meta values (charity and amount)
  /*
  $charity_id = esc_html( get_user_meta( $user_id, 'charity' , true ) );
  $charity = get_the_title( $charity_id );
  $donation_amount = esc_html( get_user_meta( $user_id, 'more_donation' , true ) );
  */
  global $wpdb;

  // get charity id
  $charity_meta_key = "charity";
  $charity_id = $wpdb->get_var( 
      $wpdb->prepare( 
          "SELECT meta_value 
          FROM $wpdb->usermeta 
          WHERE user_id = %d 
          AND meta_key = %s",
          $user_id,
          $charity_meta_key
      ) 
  );

  // get charity name
  $charity = get_the_title( $charity_id );

  // get donation amount
  $donation_meta_key = "more_donation";
  $donation_amount_db = $wpdb->get_var( 
      $wpdb->prepare( 
          "SELECT meta_value 
          FROM $wpdb->usermeta 
          WHERE user_id = %d 
          AND meta_key = %s",
          $user_id,
          $donation_meta_key
      ) 
  );
  $donation_amount = $donation_amount_db;
?>

The commented out get_user_meta returns the same output as the $wpdb. How would I go about solving my issue - it is a cache issue? Any point in the right direction will be greatly appreciated.

First access your database and see if the new data is actually in the right user metakey.
If it is there, then try using this code

$userid = get_current_user_id();

$user = get_user_by('ID', $userid);

echo $user['donation_meta_key'];

The above code should be able to print the details you are looking for. But if the old data is being returned then disable some caching plugins if you installed any.

Hi @pandglobal

It is the correct meta key. It’s a refresh issue which I’m busy sorting out. Thanks.