PHP警告:mysqli :: real_escape_string()期望参数1为字符串,给定

时间:2016-07-13 06:05:51

标签: php mysqli

我收到PHP的警告:

  

PHP警告:mysqli :: real_escape_string()期望参数1为字符串,数组为..

发出警告的代码是:

$saveData = array_map(array($connection, 'real_escape_string'), $saveData);

$ saveData是从$ _POST中获取的JSON解码 - 典型数据集将是:

array(22) {
  ["quoteID"]=>
  string(7) "GLY0YQ5"
  ["Inventory"]=>
  array(0) {
  }
  ["pickupLocation"]=>
  string(15) "EPPING NSW 2121"
  ["dropOffLocation"]=>
  string(17) "RICHMOND VIC 3121"
  ["pickupSuburb"]=>
  string(6) "EPPING"
  ["pickupPostcode"]=>
  string(4) "2121"
  ["pickupState"]=>
  string(3) "NSW"
  ["dropoffSuburb"]=>
  string(8) "RICHMOND"
  ["dropoffPostcode"]=>
  string(4) "3121"
  ["dropoffState"]=>
  string(3) "VIC"
  ["pickupLatitude"]=>
  string(10) "-33.772549"
  ["pickupLongitude"]=>
  string(10) "151.082365"
  ["dropoffLatitude"]=>
  string(10) "-37.818587"
  ["dropoffLongitude"]=>
  string(10) "144.999181"
  ["pickupDistance"]=>
  string(1) "0"
  ["pickupAccess"]=>
  string(1) "0"
  ["dropoffDistance"]=>
  string(1) "0"
  ["dropoffAccess"]=>
  string(1) "0"
  ["regionalLoading"]=>
  string(1) "0"
  ["totalVolume"]=>
  string(2) "39"
  ["totalDistance"]=>
  string(3) "897"
  ["totalPrice"]=>
  string(4) "3120"
}

我的理解是问题是mysqli期望第一个参数是$connection变量,它实际上是,它与常规mysqli格式不同,因为需要格式array_map

鉴于所需的结果是real_escape一次性整个阵列,而error只是warning并且实际上正在运行,是否有更好的方法来做到这一点,甚至有必要进行调整吗?

1 个答案:

答案 0 :(得分:0)

["Inventory"]=>
  array(0) {
  }

您的问题是您的数据包含一个数组,mysqli::real_escape_string无法转义该数组并触发警告。在尝试转义它们之前,请确保数组中的所有值都是标量值,而不是数组之类的复杂值。