I am trying to learn php and want to use a function to protect form agains SQL injection!
But somehow form record my db every data which contains any special chars like ‘”=)/()/*/
My filter function:
function filter($data) {
$data = trim(htmlentities(strip_tags($data)));
if (get_magic_quotes_gpc())
$data = stripslashes($data);
$data = mysql_real_escape_string($data);
return $data;
}
Register Page to get POST datas:
foreach($_POST as $key => $value) {
$data[$key] = filter($value);
}
Then i am trying special characters and form save them! What i an doing wrong?
If you want to protect against SQL injection, the best approach is to use PDO and prepared queries, where all user-provided data is passed in via
execute(), like this:You do not have to perform any manipulation on
$aor$b; PDO will bind the parameters the right way, no matter which database you are using.