How to Fix SQL Injection Vulnerabilities — Complete Developer Guide
SQL injection remains one of the most dangerous and prevalent web vulnerabilities, appearing in OWASP Top 10 every year since 2010. The good news: SQL injection is 100% preventable with the right coding practices. This guide covers detection, prevention, and testing.
What is SQL Injection?
// VULNERABLE - never do this $query = "SELECT * FROM users WHERE username = '" + $_GET['user'] + "'";If an attacker sends
admin' OR '1'='1' -- as the username, the query becomes SELECT * FROM users WHERE username = 'admin' OR '1'='1' --', which returns all users.Fix #1: Use Parameterized Queries (Prepared Statements)
// SAFE - parameterized query (PHP/PDO)
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
// SAFE - parameterized query (Python)
cursor.execute("SELECT * FROM users WHERE username = %s", (username,))
// SAFE - parameterized query (Java)
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE username = ?");
stmt.setString(1, username);Fix #2: Use an ORM
// Laravel Eloquent - SAFE
$user = User::where('username', $username)->first();
// SQLAlchemy - SAFE
user = session.query(User).filter(User.username == username).first();Fix #3: Input Validation and Allowlisting
Fix #4: Principle of Least Privilege for Database Users
Fix #5: Deploy a Web Application Firewall (WAF)
Testing for SQL Injection
Frequently Asked Questions
Can SQL injection affect modern frameworks?
Yes. While modern ORMs protect against basic SQL injection, custom queries or raw SQL within frameworks can still be vulnerable. Always use parameterized queries even when using Django, Rails, Laravel, or other frameworks.
How do I test if my site has SQL injection?
Use VulnScan's free SQL injection scanner at vulnscan.tech/sql-injection-scanner. Enter your domain and we check for SQLi vulnerabilities in 60 seconds. For deeper testing, use OWASP ZAP or sqlmap.
Is SQL injection still common in 2026?
Yes — SQL injection remains in the OWASP Top 3 and is responsible for approximately 8% of data breaches. Legacy systems, WordPress plugins, and poorly maintained codebases continue to introduce new SQL injection vulnerabilities.