✏️ Explanatory Question
Level: Expert — Security is critical; SQL injection remains one of the most common and dangerous vulnerabilities.
SQL Injection (SQLi) is an attack where a malicious user inserts (injects) harmful SQL code through application input fields, tricking the database into running unintended commands — potentially exposing, altering, or deleting data.
' OR '1'='1-- VULNERABLE (never do this — string concatenation)
-- "SELECT * FROM users WHERE email = '" + userInput + "'";
-- Input: ' OR '1'='1 -> returns ALL users!
-- SAFE: MySQL prepared statement with a placeholder
PREPARE stmt FROM
'SELECT * FROM users WHERE email = ? AND status = ?';
SET @email = 'rumman@example.com';
SET @status = 'Active';
EXECUTE stmt USING @email, @status; -- input is bound as data, not SQL
DEALLOCATE PREPARE stmt;
-- Create an app user with ONLY the needed privileges
CREATE USER 'app_user'@'%' IDENTIFIED BY 'Str0ng!Pass';
GRANT SELECT, INSERT, UPDATE ON shop.orders TO 'app_user'@'%';
-- (No DROP, no GRANT, no access to other databases)
-- Store password hashes, never plain text
INSERT INTO users (email, password_hash)
VALUES ('a@x.com', SHA2('userPasswordHere', 256));
-- Require encrypted (TLS) connections for a user
ALTER USER 'app_user'@'%' REQUIRE SSL;