SQL injection demonstrated

SQL injection is a technique used to gain access to the content of a database without providing the correct user credentials by inputting bits of PHP code using the username or password field of a login form in such a way that the logic of the SQL query is altered.

PHP scripts which provide access to databases should be written in such a way that SQL injection is difficult if not impossible. At present our script, ‘display.php’, which returns records from the database ‘test_db’, is wide open to such an attack, and in this lesson I demonstrate how this is done.