mysql_real_escape_string and array_map returns blank strings?

本秂侑毒 提交于 2019-12-02 02:25:27

array_map returns new array, if you're overwriting $_POST, better solution would be to use array_walk.

array_walk($_POST, function(&$string) use ($link) { 
  $string = mysqli_real_escape_string($link, $string);
});

Note that $link must be valid connection.

Function [ <internal:mysqli> function mysqli_real_escape_string ] {

  - Parameters [2] {
    Parameter #0 [ <required> $link ]
    Parameter #1 [ <required> $string_to_escape ]
  }
}

You must pass values ​​escaped to another variable:

$post = array_map('mysqli_real_escape_string', $_POST);

Or:

foreach($_POST as $k => $v) {
    $_POST[$k] = mysqli_real_escape_string($v);
}

Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.

Then yes this is your answer:

mysql_real_escape_string() requires a connection to the database as it uses the database's character set to determine what is needed to be escaped.
Without this, PHP has no idea what char set you're using and so what to escape.

There are many:
http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html
And all with different chars.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!