Duplicate values appended each time save checkbox values

I am trying to create a multiselect dropdown list using “jQuery UI MultiSelect Widget” (http://www.erichynds.com/blog/jquery-ui-multiselect-widget) on WordPress template page and save checked options into database (Mysql) as a single string value.

And THE PROBLEM: duplicate values are appended (i.e A, B, A, B) each time I click Save form. The problem does not happen IF I select new options in this dropdown list. BTW, other fields like textbox, single-select dropdown or textarea are OK.

Read More

Below are 2 code segments that I believe the problem comes from, so someone please help me point out what’s going wrong here. I have been worked around this problem for the whole week but NO luck! Thank you very much!

    <?php
case 'maker_dropdown':
    global $wpdb;
    $makers = $wpdb->get_var( $wpdb->prepare( "SELECT field_values FROM ". $wpdb->prefix . "cp_ad_fields WHERE field_name = 'cp_maker';", "" ) );
    $selected_makers = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM ". $wpdb->prefix . "usermeta WHERE meta_key = 'maker' AND user_id =" . $user->ID . ";", "" ) );

    if ($makers) {
?>
   <select name="<?php echo $field_id; ?>[]" class="regular-dropdown multiselect"  multiple="multiple" >
<?php                           
   $options = explode( ',', $makers);  
   $selected_options = explode( ',', $selected_makers);  

   foreach ( $options as $option) { // loop thru all available options
       if ( in_array($option, $selected_options)) { 
?>
           <option name="<?php echo $option; ?>[]" selected="selected" value="<?php esc_attr_e($the_value); ?>"><?php esc_attr_e($option); ?></option>
<?php
       }
       else {
?>
           <option  value="<?php esc_attr_e($option); ?>"><?php esc_attr_e($option); ?></option>
<?php
       }
    } //endforeach
?>
    </select>
<?php } //endif ?>
break;

This code segment is used to display multiselect dropdown list on front-end.

function ctm_profile_fields_save($user_id) {
global $ctm_extra_profile_fields;

foreach ($ctm_extra_profile_fields as $field_id => $field_values) :
    $selected_options = implode(',', $_POST[$field_id]);

    if ($field_values['type'] == 'maker_dropdown') {
        update_user_meta( $user_id, $field_id, $selected_options );
    }
    else { 
        update_user_meta( $user_id, $field_id, sanitize_text_field(  $_POST[$field_id] ) ); 
    }
endforeach;
}
add_action('personal_options_update', 'ctm_profile_fields_save');
add_action('edit_user_profile_update', 'ctm_profile_fields_save');

This code segment is used to save multiselect dropdown list into database.

Related posts

Leave a Reply

1 comment

  1. update_user_meta function allows for multiple meta entries with the same key.

    When you read the existing makers for the user you may get [‘A’, ‘B’]. When you save the submitted form you add a new set of selected values [‘A’, ‘B’] to what was there previously. This is why your values are duplicated.

    There are two easy ways of solving this.

    Option 1: Clear the meta key before saving it.

    foreach ($ctm_extra_profile_fields as $field_id => $field_values) :
        $selected_options = implode(',', $_POST[$field_id]);
    
        delete_user_meta($user_id, $field_id);
    
        if ($field_values['type'] == 'maker_dropdown') {
            update_user_meta( $user_id, $field_id, $selected_options );
        }
        else { 
            update_user_meta( $user_id, $field_id, sanitize_text_field(  $_POST[$field_id] ) ); 
        }
    endforeach;
    

    Option 2: Specify previous value in the update_user_meta to not create duplicate entries

    foreach ($ctm_extra_profile_fields as $field_id => $field_values) :
        $selected_options = implode(',', $_POST[$field_id]);
    
        if ($field_values['type'] == 'maker_dropdown') {
            update_user_meta( $user_id, $field_id, $selected_options, $selected_options );
        }
        else { 
            update_user_meta( $user_id, $field_id, sanitize_text_field(  $_POST[$field_id] ), sanitize_text_field(  $_POST[$field_id] ) ); 
        }
    endforeach;