SQL injection (SQLi) is one of the most dangerous and most common web attacks. It targets the database behind your website, and a successful attack can expose every record you store. Its browser-side counterpart is cross-site scripting.

How SQL injection works

Websites build database queries using input from users: a search box, a login form, a URL parameter. If that input is pasted into the query as text, an attacker can send input that changes the query itself. Instead of searching for a product, the query might dump your entire users table, or delete it.

The root cause is always the same: the application mixes code (the SQL) and data (the user's input) in one string, so the database can't tell them apart.

A simple example

A login checks WHERE username = '<input>'. If the attacker types ' OR '1'='1, the condition is always true, and they may log in without a password.

In PHP, the vulnerable version looks like this:

$sql = "SELECT * FROM {$wpdb->users} WHERE user_login = '" . $_POST['user'] . "'";
$user = $wpdb->get_row( $sql );

The same query written safely, with a placeholder:

$user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->users} WHERE user_login = %s", $_POST['user'] ) );

Now the input is only ever treated as a value, whatever it contains.

What attackers get

  • usernames and password hashes;
  • customer data and order details;
  • payment details, if the site stores them;
  • the ability to create admin accounts;
  • a way to plant malware, for example by writing script tags into posts or options.

Many attacks are "blind": the page shows no data, but the attacker learns the answer one yes-or-no question at a time from differences in the response or its timing. A page that shows no database errors can still be vulnerable.

How to prevent SQL injection

  1. Use prepared statements. This is the real fix. The query and the data are sent separately, so input can never change the query's structure. In WordPress, use $wpdb->prepare() for every query with variables, with %s for strings, %d for integers and %f for decimals. For table or column names that come from code, use %i (WordPress 6.2 and newer) or an allow-list; never take them from user input.
  2. Validate and sanitize input. Only accept what you expect. A ZIP code field should not accept SQL keywords. Use absint() for IDs and the sanitize_*() functions for text. Never trust values like prices or IDs either: that's parameter tampering.
  3. Give the database user least privilege. The web app rarely needs to drop tables. Give the WordPress database user rights on its own database only, and never server-wide rights such as FILE or GRANT. Keep the CREATE and ALTER rights WordPress needs for updates.
  4. Keep plugins and themes updated. Most WordPress SQLi comes through a vulnerable plugin, not your own code. Remove plugins you don't use: inactive code can still be reachable.
  5. Run a WAF. A web application firewall recognizes SQLi patterns and blocks them before they reach your database, which is useful when the hole is in a plugin you cannot fix yourself.
  6. Hide database errors. Turn off display_errors and WP_DEBUG_DISPLAY on a live site, so error messages don't show attackers how your queries are built.

Common mistakes

  • Escaping instead of preparing. addslashes() and hand-built escaping miss edge cases. esc_sql() exists for special cases, but $wpdb->prepare() is the default.
  • Putting the input in the query string anyway. $wpdb->prepare( "... WHERE id = $id" ) protects nothing. The value must be a separate argument that fills a placeholder.
  • Trusting "internal" values. Cookies, hidden form fields, HTTP headers and data from logged-in users all come from the browser and can be changed.
  • Building ORDER BY or LIMIT from the URL. These parts can't use %s. Map the input to a fixed list of allowed values instead.
  • Thinking a popular plugin is safe. Widely used plugins have had SQL-injection flaws too; what matters is that you update quickly.

What a firewall can't do

A firewall blocks requests that look like known injection attacks. It doesn't fix the vulnerable query, and a determined attacker may find a payload that no rule matches. It also can't stop a query that is logically wrong but syntactically safe, such as showing an order to the wrong customer. Fix the code or update the plugin, and treat the firewall as the layer that buys you time.

How Silent Threads helps

Silent Threads' firewall includes SQL-injection rules, and its vulnerability scanner warns you when an installed plugin has a known SQLi flaw.

The short version

SQL injection happens when input becomes part of a query. Use $wpdb->prepare() everywhere, validate what you accept, limit what the database user can do, update plugins promptly and run a firewall for the flaws you can't fix yourself.