dppb79372 2013-09-16 19:50
浏览 33
已采纳

尝试在其中加载另一个数组时,PHP数组会引发错误

I am finding the following error: "Warning: htmlspecialchars() expects parameter 1 to be string, array given..."

This happens in the following piece of code and only when I added input fields to the form whose name was an array (so I could repeat the input multiple times). The line the error refers to is ($v=htmlspecialchars($value);)

if ($len > 2) {
    $values=array();
    $possible=array('orderId','source','date', 'clientPrice','firstName','lastName','email','address','city','zip');
    $i=1;
    $query2 = "UPDATE orders SET ";
    foreach ($_POST as $key => $value) {
        $k=htmlspecialchars($key);
        $v=htmlspecialchars($value);
        if(in_array($k, $possible)) {
            $query2 .= $k." = ?";
            $values[]=$v;  //append values to an array for later use
            if($i < ($len-2)) $query2 .= ', ';
            $i++;
        }
    }
}

Any idea of how to solve this and the reason for the error?

  • 写回答

3条回答 默认 最新

  • dongweihuan8610 2013-09-16 19:54
    关注

    You answered your own question in the first paragraph. You passed an array as values, therefore $value is going to be an array of the various inputs you assigned an array name to.

    foreach($_POST as $key => $value)
    {
        if ( ! is_array($value))
        {
            // Manage values that aren't arrays
        }
    }
    

    Iterating over $_POST isn't really a good practice. You would be better assigning the actual names of the fields to their own variables or creating your own array with the exact data you need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么