Forum Moderators: coopster
Here is what I am trying:
foreach($shonet as $value){
$value = mysql_real_escape_string($value);
}
But I get this err when the script runs:
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /html etc.
Warning: Invalid argument supplied for foreach() in /html etc
What im I doing wrong here or should I be trying something different?
Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.
You can either reference the array (in PHP5) as shown in the examples on the manual page or you can assign the updated values to a different array.
If you intend to update every value in the array, one of my favorites is to use array_map [php.net].
In your example above $value must still be an array so you would need another foreach($value as $v) in there.
$array = array();
foreach($array as $key=>$a)
{
$array[$key] = mysql_real_escape_string($a);
}
print_r($array);
When you print_r $array you'll see your data that needed escaping will be escaped.
//Check if magic qoutes is on then stripslashes if needed
function codeClean($var)
{
if (is_array($var)) {
foreach($var as $key => $val) {
$output[$key] = codeClean($val);
}
} else {
$var = strip_tags(trim($var));
if (function_exists("get_magic_quotes_gpc")) {
$output = sqlEscapeString((get_magic_quotes_gpc())? stripslashes($var): $var);
} else {
$output = sqlEscapeString($var);
}
}
if (!empty($output))
return $output;
}
just pass the array through the function as is the function does all the work
$array = codeClean($some_array);