dongqigu0429 2018-05-20 05:21
浏览 98
已采纳

复杂的PHP错误:尝试转义数组

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
  • 写回答

1条回答 默认 最新

  • dongpi9164 2018-05-20 05:46
    关注

    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().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决,来真人,不要ai!
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法