Also is there another way of declaring / cleaning the submitted varaibles instead of having to type out each form field like above to clean it?
It would be greate to to be able to access the name variable ‘$name’ without having to type out that full declaration / cleaning code (considering my form might have 20 fields)
I like to use the filter extension that comes with PHP and a prepared statement at the query level. Here’s a simple example of a few filters and sanitizers I’ve used. I’m sure there’s a more dynamic way to set theses up, but this worked for my situtation…
You’re more or less escaping rather than “cleaning.” I would recommend doing the escaping while performing the query because it offers a number of advantages. For one, you don’t have to type all the fields out manually beforehand, second, you can’t accidentally pass the non-escaped version (if you do it right), and third, less variables to keep around in your head.
Of course, doing it right is key. The idea is to make it difficult to make an error. You have two main options off the top of my head: Use “prepared statements”:
$stmt = $someSQLLibrary->newQuery("SELECT * FROM users WHERE username = ?");
$stmt->execute(array($username));
…or create a function that takes format specifiers and automatically escapes:
queryf("SELECT * FROM users WHERE username = %s", $username))