Pattatharasu Nataraj
Pattatharasu Nataraj

Reputation: 248

wordpress update meta not working

hey i am trying to update user meta but its not updating

  $dobs=$_REQUEST['day'].'_'.$_REQUEST['month'].'_'.$_REQUEST['year'];
  $gender=$_REQUEST['gender'];
  $country=$_REQUEST['country'];
  $state=$_REQUEST['state'];
  $city=$_REQUEST['city'];

  update_user_meta( $user_id, 'date_of_birth', $dobs, true );
  update_user_meta((int) $user_id, 'gender', (int) $gender, true );
  update_user_meta( $user_id, 'country', $country, true );
  update_user_meta( $user_id, 'state', $state, true );
  update_user_meta( $user_id, 'city', $city, true );

here i tried typecasting like (int) (string) interesting part is insert working properly don't know if there is an error in DB structure.

Upvotes: 0

Views: 647

Answers (2)

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

update_user_meta() - Update user meta field based on user ID. It has only three arguments in general where the 4th is optional. The 4th parameter is the $prev_value which in default will be the '' values alone. Hence the 4th parameter will be checking whether the meta_key updated has the previous values to it.

For Example:

<?php
     $user_id = 1;
     $website_url = 'http://wordpress.org';
     update_user_meta($user_id, 'user_url', $website_url, TRUE); 
    // The above statement will update the user_meta table corresponding to the user_id=1
?>

Note: The First 3 Parameters are required. Without that it will not work.

Kindly ensure that the the 'second value' in the update_user_meta() should be existing into the database table.

As per the Question Author Scenario:

update_user_meta( $user_id, 'date_of_birth', $dobs, true );

It must have all the 3 parameters namely $user_id,date_of_birth and the $dobs.

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32354

You need to remove the 4th param of the update_user_meta

update_user_meta( $user_id, 'date_of_birth', $dobs );

The 4th param is used by wordpress to update only the fields that have a previous value equal with the value of the 4th param

So wordpress is looking for date_of_birth that has a previous value of true for that user

more info

Upvotes: 2

Related Questions