【问题标题】:error during addslashes() function in phpphp中addslashes()函数期间的错误
【发布时间】:2011-03-30 20:02:16
【问题描述】:

html表单代码-

<td width="75">
<input name="txtQty[]" type="text" id="txtQty[]" size="5" 
 value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">

当我提交表单时,我会调用以下脚本-

if (!get_magic_quotes_gpc()) {
if (isset($_POST)) {
    foreach ($_POST as $key => $value) {
        $_POST[$key] =  trim(addslashes($value));
    }
}

if (isset($_GET)) {
    foreach ($_GET as $key => $value) {
        $_GET[$key] = trim(addslashes($value));
    }
}   
}

错误-

警告:addslashes() 期望参数 1 是字符串,数组在 C:\xampp\htdocs\shizin\products\library\config.php 第 53 行给出

我认为这个脚本只是用来修剪输入,但我不知道这个 addlash 函数的作用以及为什么会出现这个错误。

【问题讨论】:

  • addslashes() 只返回一个在预定义字符前带有反斜杠的字符串。
  • 关于您收到的错误..确保 $value 是一个字符串...
  • 错误信息不是很明显吗? $value 不是字符串。

标签: php trim addslashes


【解决方案1】:

如果您将此代码应用于 int 值,则您可以像这样删除这些函数

if (!get_magic_quotes_gpc()) { 
if (isset($_POST)) { 
    foreach ($_POST as $key => $value) { 
        $_POST[$key] =  $value; 
    } 
} 

if (isset($_GET)) { 
    foreach ($_GET as $key => $value) { 
        $_GET[$key] = $value; 
    } 
}    
} 

【讨论】:

  • 谢谢 Chandraveer singh,那个工作。顺便说一句,代码 sn-p 来自 plaincart 电子商务购物车教程。
【解决方案2】:
  1. 整个方法都是错误的。
    收到用户提供的数据后,您必须去除斜线,由魔术引号添加,而不是添加。

  2. 关于数组方法它说 2 个答案已经发布,我希望在这里得到很好的解释。 不太好,但无论如何。

因此,您将需要 2 个代码 sn-ps。
第一个是来自http://www.php.net/manual/en/function.stripslashes.php的stripslashes_deep()

在您告诉我们您为什么认为需要您发布的代码后,您将获得第二个。

【讨论】:

    【解决方案3】:

    错误说,addslashes 函数尝试用斜杠引用字符串, 但是参数不是字符串的$value是一个数组$_GET 包含什么?

    这是因为调用此脚本的页面传递了一个数组。 txtQty[]

    http://php.net/manual/en/function.addslashes.php

    【讨论】:

      【解决方案4】:

      在将 $value 传递给 addlashes() 之前,只需 echo $value,那么您应该会立即看到问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-13
        • 1970-01-01
        • 1970-01-01
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多