Back to cheatsheets
Developer
SQL Injection Prevention
Prevent SQL injection with parameterized queries, ORM best practices, and input validation.
What you'll learn
- Parameterized queries
- ORM safe patterns
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
Code Snippets
Copy-paste code snippets
2 ready-to-use snippets for sql injection prevention. Click copy and paste into your project.
Parameterized queries
// ❌ NEVER concatenate SQL
const query = `SELECT * FROM users
WHERE name = '${userInput}'`;
// Input: ' OR '1'='1 → returns all users
// ✅ Use parameterized queries (Node.js + pg)
const query = 'SELECT * FROM users WHERE name = $1';
const result = await pool.query(query, [userInput]);
// ✅ Prepared statements (MySQL)
const stmt = await conn.prepare(
'SELECT * FROM users WHERE name = ?'
);
const result = await stmt.execute([userInput]);
ORM safe patterns
// Prisma — safe by default
const user = await prisma.user.findFirst({
where: { name: userInput }, // parameterized
});
// Sequelize — safe with bindings
const user = await User.findOne({
where: { name: userInput }, // parameterized
});
// ❌ NEVER use raw queries without bindings
await prisma.$queryRaw(
`SELECT * FROM users WHERE name = '${input}'`
); // DANGEROUS!
// ✅ Raw with bindings
await prisma.$queryRaw(
'SELECT * FROM users WHERE name = $1', input
); // Safe
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site