jarvis Posted January 19, 2016 Share Posted January 19, 2016 Hi all, I'm running an update sql query from my wordpress functions file when a form is submitted. This grabs the address fields, uses the Google API and gets the lat and lng co-ords so I can utilise these elsehwere. My issue is the warning I get when I submit the form. My codes below: $meta_value = array( 'address' => $full_address, 'lat' => $lat, 'lng' => $lng ); #$location = array($full_address,$lat,$lng); print_r($meta_value); $wpdb->show_errors(); #$wpdb->update( $table, $data, $where ); $wpdb->update( $wpdb->postmeta, array( 'meta_value' => $meta_value ), array( "meta_key" => 'location', "post_id" => $post_id )); $wpdb->print_error(); var_dump( $wpdb->last_query ); However, when it runs, I get the warning: Warning: mysql_real_escape_string() expects parameter 1 to be string, It should pass in the values from the Array: Array ( [address] => Address 1, Town, County AB12 3CD, United kingdom [lat] => 50.123456 [lng] => 0.1234567 ) I can't see for looking now so any help is much appreciated! Quote Link to comment Share on other sites More sharing options...
jarvis Posted January 19, 2016 Author Share Posted January 19, 2016 Ah think I'm getting somewhere (typical after posting on here and a day of hair pulling) I've changed the code to $meta_value = serialize(array( 'address' => $full_address, 'lat' => $lat, 'lng' => $lng )); Which has resolved the warning. But now adds \\ which I need to remove. For example; a:3:{s:7:"address";s:55:"1 test road, Test town, Test County ab12 3cd, UK";s:3:"lat";d:50.123456;s:3:"lng";d:0.123456;} How would I remove those? Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted January 19, 2016 Solution Share Posted January 19, 2016 According to the manual for the update method, the second parameter should be an array of name/value pairs. Instead you are passing a name/array. 'meta_value' is supposed to be a field name and $meta_value is assumed to be a value for that field. You are passing an array for for $meta_value. You can't store an array as the value for a field. Not knowing exactly what you are trying to achieve, I can't provide the correct solution. I'm thinking you should either be storing the data in fields named 'address', 'lat', and 'long' using this $wpdb->update( $wpdb->postmeta, $meta_value, array( "meta_key" => 'location', "post_id" => $post_id ) ); Or, you may need to convert the array to a string. Definitely not advised to store data in this manner, but since you are using word-press, I'm not sure what your options are $value = implode(", ", ); $wpdb->update( $wpdb->postmeta, array('meta_value' => $value), array( "meta_key" => 'location', "post_id" => $post_id ) ); Quote Link to comment Share on other sites More sharing options...
jarvis Posted January 19, 2016 Author Share Posted January 19, 2016 Ah thanks Psycho, will try option 1 Thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.