SQL injection is one of the oldest web vulnerabilities and still one of the most dangerous, because the database holds everything: users, password hashes, orders, customer details. One badly written search field is enough to expose it.
Example: vulnerable code
$id = $_GET['id'];
$result = $db->query("SELECT * FROM products WHERE id = " . $id);
When a visitor opens product.php?id=5, it works as expected. But an attacker types something else instead of the number:
What the attacker puts in id |
Resulting query | Effect |
|---|---|---|
5 OR 1=1 |
... WHERE id = 5 OR 1=1 |
Returns every product |
5 UNION SELECT user_login, user_pass FROM wp_users |
Merges the users table into the result | Exposes usernames and password hashes |
5; DROP TABLE orders |
A second command after the first | Deletes the orders table if the driver allows multiple statements |
The fix: prepared statements
$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
$stmt->execute([$_GET['id']]);
$product = $stmt->fetch();
The query goes to the database first with a ? placeholder, and the value is sent separately. Whatever the attacker types is treated as a value, never as a command.
In popular systems:
- WordPress: use
$wpdb->prepare()with%dfor numbers and%sfor strings. - Laravel: Eloquent and the query builder bind values automatically, but beware of concatenating input into
whereRaworDB::raw.
Extra layers of protection
- Least privilege: the site’s database user usually doesn’t need permission to drop tables.
- Hide error messages from visitors, because they reveal your table structure to attackers.
- A web application firewall (WAF) to block known attempts.
- Keep plugins and libraries updated.
On ArabyHost
Imunify360 security on ArabyHost servers blocks many known injection patterns before they reach your site, and CageFS isolates every account so an attacker can’t reach other accounts’ databases. Fixing the code is still your job, and with daily JetBackup backups you can restore the database if it’s tampered with. For more, follow the protect your site from hacking guide.
FAQ
Isn't addslashes or input sanitising enough?
No. Hand-rolled escaping leaves gaps in edge cases such as unquoted numbers or character encodings. Prepared statements keep data separate from the command itself, which is the reliable fix.
Is WordPress vulnerable to SQL injection?
WordPress core uses wpdb->prepare, but badly written plugins and themes can contain the flaw. Keep plugins updated and delete the ones you don't use.
Does a WAF protect against SQL injection?
It blocks known patterns and lowers the risk, but an attacker may find a payload the rules don't match. A WAF is an extra layer; the real fix is in the code.