Is there a generic way to prevent XSS on a form?
Is we consider the basic sample code below...
HTML:
<form action="test.php" method="POST">
<input type="text" name="test">
</form>
PHP:
function new_test($test) {
array_walk($test, 'array_sanitise');
$fields = '`' . implode('`, `', array_keys($test)) . '`';
$data = '\'' . implode('\', \'', $test) . '\'';
$query = mysql_query("INSERT INTO `test` ($fields) VALUES ($data)");
if (!$query) {
die('Could not query:' . mysql_error());
}
}
$test = array(
'test' => $_POST['test']
);
new_test($test);
Is this enough to protect from SQL injections & XSS breaches? Is there a better way to do it? Or is there no generic way?