Jump to content

Warning: mysql_real_escape_string() expects parameter 1 to be string,


jarvis
Go to solution Solved by Psycho,

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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
    )
);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.