views:

121

answers:

1

Possible Duplicates:
What is SQL injection?
What is the best way to avoid SQL injection attacks?

Hi All,

Could you please help me in learning sql injection attack.

Please provide me with the code which is vulnerable to sql injection and the one which is not.

Thanks in advance

+2  A: 

The following code is vulnerable to SQL injection:

mysqli_query( $link,
              'SELECT Name FROM User WHERE UserID = '.$_POST['UserID']
              );

The following code is not vulnerable to SQL injection:

mysqli_query( $link,
              'SELECT Name FROM User WHERE UserID = \''.
                  mysqli_real_escape_string($link, $_POST['UserID']).
                  '\''
              );

Notwithstanding the fact that this second snippet is not vulnerable, it is better to adopt a systematic way of keeping yourself safe from injection than just to manually escape strings. Read about parameterised statements.

Hammerite
+1 for trying to answer the question instead of pasting a "little bobby tables" reference.
FrustratedWithFormsDesigner