In this article, we are going to discuss how to prevent SQL injection in PHP. The prerequisite of this topic is that you are having XAMPP in your computer.
Why SQL injection occurs?SQL injection is a code injection technique used to attack data-driven applications, in which malicious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker).
In this type of technique, the hacker or attacker uses some special characters which convert the SQL query into a new SQL query and the attacker can manipulate the query by entering more kinds of keywords.
Let us make a SQL injection scenario then we will learn how to fix it.
Step 1: So, let's start by creating a database CREATE DATABASE GFG;
Step 2: Use this database USE GFG;
Step 3: Create a login credentials table in GFG database CREATE TABLE users( id int(10) PRIMARY KEY AUTO_INCREMENT, username VARCHAR(255), password VARCHAR(255) );
Step 4: Insert some data into database INSERT INTO users VALUES(1, 'idevesh', '1234'); INSERT INTO users VALUES(2, 'geeksforgeeks', 'gfg');
Data After insertion
Step 5: Now create a PHP script for login page
(a) Create a DB Connection File (dbconnection.php)