Reputation: 312
I'm trying to mysqli escape an array mysqli_real_escape_string function. But it won't work because it expects a string. So I use this :
$items = array_map('mysqli_real_escape_string', $link, $_POST['items']);
where $link is my database connection and $_POST['items'] is the array. But I get this error :
Warning: array_map(): Argument #2 should be an array
So I changed it to :
$items = array_map('mysqli_real_escape_string', $_POST['items']);
where the 2nd argument is an array and then I get this message :
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given
So I changed it again to :
$items = array_map('mysqli_real_escape_string', $_POST['items'], $link);
but now I got this :
Warning: array_map(): Argument #3 should be an array
After some research, I tried this codes :
$items = array_map('mysqli_real_escape_string', $_POST['items'] [,$link]);
$items = array_map('mysqli_real_escape_string($link)', $_POST['items']);
but none of the worked.
Any help is appriciated. Thanks.
EDIT : I've tried this :
function escape_array($array){
global $link;
mysqli_real_escape_string($link, $array);
return;
}
$items = array_map('escape_array', $_POST['items']);
echo $items;
which should call the mysqli_real_escape_string with the connection but I get this error instead :
( ! ) Notice: Array to string conversion in D:\wamp64\www\sellerpanel\test.php on line 21
Call Stack
# Time Memory Function Location
1 0.0007 370968 {main}( ) ...\test.php:0
Array
Upvotes: 0
Views: 65
Reputation: 781059
You can do it with an anonymous function that imports the $link
variable.
$items = array_map(function($x) use ($link) {
return mysqli_real_escape_string($link, $x);
}, $_POST['items']);
However, you really shouldn't be using mysqli_real_escape_string
in the first place. Use a prepared statement and mysqli_stmt_bind_param()
.
Upvotes: 1