21
May

Something i recently discovered, and already regret I didn’t knew this before, makes it much more easier to parse $_POST or $_GET variables into ordinary variables so i can use it properly for mysql insertions.

This is what i used to do:

<?php
   $var1 = mysql_real_escape($_POST['var1']);
   $var2 = mysql_real_escape($_POST['var2']);
   $var3 = mysql_real_escape($_POST['var3']);
?>

Ofcourse we all can see the repeat in this, but i never figured out how to solve that issue.
But ofcourse there is always someone else with a better idea, and I like to share it with you

<?php
  foreach ($_POST as $key => $value){
    $$key = mysql_real_escape_string($value);
  }
?>

And thats simply all. It will put all the $_POST array items into a numerous variables.
The double “$” does what you expect it to do, it makes a variable out of the content of the variable.

When $a = test, than $$a = test2 will result into $test = test2.
It is that easy!

, , , , ,

Add reply