Web security
SQL Injection Explained: How It Works and How to Prevent It

SQL injection is one of the oldest web vulnerabilities, and it still turns up in real applications. It happens when an application builds a database query by pasting user input into it, so specially crafted input changes what the query does. This guide explains the idea, the main variants and, most importantly, the fix.
How It Works
Imagine a login that builds the query "find the user whose name is X and password is Y" by joining text together. If someone types input containing a quote and extra SQL, the database may read part of their input as instructions instead of data, for example making the password check always true. The flaw is not the database; it is mixing code and data.
What an Attacker Can Do
- Log in as another user without a password
- Read data from other tables, such as customer records
- Change or delete data
- In some configurations, read files or run commands on the server
Main Variants
- In-band, error-based: database errors in the page reveal information
- In-band, UNION-based: the attacker adds their own query and sees its results in the page
- Blind, boolean-based: the page changes slightly depending on whether a condition is true
- Blind, time-based: the attacker makes the database pause and measures the delay
- Out-of-band: the database is made to contact a server the attacker controls
How Testers Find It
Testers look at every place input reaches the server: form fields, URL parameters, cookies, headers and JSON bodies in APIs. They send characters that would break a query and watch for errors, differences or delays, usually in Burp Suite's Repeater. Tools such as SQLmap automate confirmation, but only within scope and agreed limits, because they can send a lot of traffic. Practice only in labs such as DVWA or PortSwigger's Web Security Academy.
How to Prevent It
- Parameterized queries (prepared statements) for every query that uses input. This is the real fix
- Use ORM and query-builder features correctly; raw query functions can reintroduce the flaw
- Give the application's database account only the permissions it needs
- Validate input types and lengths as an extra layer, not the main defense
- Show generic error messages to users and log the details privately
Where It Sits in OWASP
SQL injection belongs to the Injection category, A05 in the OWASP Top 10:2025. Cross-site scripting is a close relative; see XSS explained.
Learning Web Testing
Injection testing is part of our Application Security course, where you find, confirm and report flaws like this in authorized labs, then retest the fix. In the OWASP lists SQL injection sits under Injection, which is A05:2025 and was A03:2021; see what changed.
Want to learn web testing step by step? Book free trial classes. Contact us or call +91 86868 92922.
Where This Guide Fits in Our Training
- Web Application Penetration Testing Course in Hyderabad: the web application testing course and how it is taught
- API Security Training Online: REST, SOAP and GraphQL testing, with Burp Suite
- Cyber Security Course with Practical Labs in Hyderabad: how the labs work and what you practise in each class
Courses Mentioned in This Guide
More Guides
ffuf Commands for Beginners: Find Hidden Web Paths in a Lab
ffuf commands and cheat sheet: find directories and files, use filters and rate limits and read the results, with examples for a lab you own.
Read the guide →sqlmap for Beginners: Testing SQL Injection in a Lab
Learn sqlmap step by step: test a URL parameter, list databases and tables, use cookies and keep tests safe, with commands for a lab you own.
Read the guide →OWASP ZAP for Beginners: A Free Web Vulnerability Scanner
Learn OWASP ZAP step by step: proxy your browser, spider a site, run passive and active scans, read alerts and use the baseline script, in a lab you own.
Read the guide →Cross-Site Scripting (XSS) Explained for Beginners
XSS in plain language: reflected, stored and DOM-based cross-site scripting, what an attacker can do, how testers find it, and how developers prevent it.
Read the guide →Start Your Cyber Security Career
Talk to a counselor, choose the right course for your goals, and get the fee, syllabus and next batch dates in writing.
- Call+91 86868 92922
Open 7 days: Mon to Fri 8:30 AM to 8:30 PM IST
Sat and Sun 10 AM to 6 PM IST - Emailinfo@hackingtrainer.com
- VisitGround Floor, G-3, Sri Venkatasai Nest, Kallu Compound Road, Nagarjuna Nagar Colony, Yella Reddy Guda, Ameerpet, Hyderabad – 500073
Open in Google Maps