Allright, I wrote this little snippet to prevent mysql injection.
And then I thought of the fact that alot of websites are still sensitive to mysql-injection, hence I came on the idea of sharing this script.
<?php
function sql_query($query, $variables)
{
//Take the query, and replace the contained variables
//Query should contain [WHERE]
if ( !preg_match('[WHERE]', $query) )
{
die();
}
else
{
$parms = Array();
foreach( $variables as $name => $variable )
{
$parms[] = "`".$name."` = '".mysql_real_escape_string($variable)."'";
}
//Merge parameters
$parameters = implode(' AND ', $parms);
//Prepare the query
$query2 = str_replace('[WHERE]', 'WHERE '.$parameters, $query);
return mysql_query($query2);
}
}
?>
And the usage:
<?php
include('./sql_query.php'); //Or any way to implement the function.
$query = sql_query('SELECT * FROM `table` [WHERE]', Array(
"field1" => $_POST['username'],
"field2" => 'value'
));
?>
In the array, the key name is the field name in SQL.
Then the value in the array is the one that should be found.
For now, I do not support the OR attribute, and I never use that one personally xD