0

I have $_POST with 20 keys. I want to apply mysqli_real_escape_string() for the entire $_POST array. So, should I apply mysqli_real_escape_string() to all 20 keys separately? Or is there any loop or any specific function available for it?

My Post array is like the example below:

$_POST =array(
      "A1"=>'xxxxxxxx',
      "A2"=>'xxxxxxxx',
      ........
      ........
      ........
      ..........
      .........
      .........
      "A20"=> 'xxxxxxxxx'
);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Bhavesh Patel
  • 115
  • 1
  • 2
  • 12
  • 5
    **STOP!!** "I want to apply mysqli_real_escape_string() for entire $_POST array" — That is a bad solution to pretty much any problem. Data should be escaped at **the last moment** before being used for whatever it is being used for (i.e. just before being inserted into SQL not just after it comes out of HTTP). What's more, mysqli_real_escape_string should be avoided in favour of bound variables / prepared statements. – Quentin May 17 '18 at 09:09

1 Answers1

-2

You can do something like this:

$escaped = array_map(function($var) use ($mysqli){
    return mysqli_real_escape_string($mysqli, $var);
},$_POST);
Domenik Reitzner
  • 1,583
  • 1
  • 12
  • 21