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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序