Jump to content

Will this prevent a SQL injection?


crmamx

Recommended Posts

Will this prevent a SQL injection? I am guessing the answer is no because it is too simple.

 

// retrieve form data   ==========================================

$ama = $_POST['ama'];

// Check for alphanumeric characters =====================================

$string = "$ama";

$new_string = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);

// echo $new_string;

// Send query ===========================================================

$query = "SELECT * FROM members WHERE ama='$new_string'";
if (!mysql_query($query)){
die('Error :' .mysql_error());
}

Link to comment
https://forums.phpfreaks.com/topic/228389-will-this-prevent-a-sql-injection/
Share on other sites

Yes it will but your code can be shortened even further:

 

$ama = preg_replace('#[^a-zA-Z0-9\s]#', '', $_POST['ama']);

// echo $ama;

if (!mysql_query("SELECT * FROM members WHERE ama = '$ama'"))
    die('Error :' . mysql_error() . '.');
else
    die('Query was successful.');

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.