Skip to content
FunnelGuard

Fix guide by FunnelsGuard

SQL injection in forms: how to find and fix it

OPS-179High impactOther toolBy FunnelGuardPublished Updated
On this page

What this issue is

SQL (Structured Query Language) is the language websites use to talk to their database. SQL injection happens when a form field, such as a name, email, search box or coupon code, is pasted directly into a SQL query. An attacker types SQL code instead of normal text, and the database runs it as a command.

Forms are a common target because they take free text from anyone on the internet and often save it to a database. Contact forms, login forms, search boxes, booking forms and checkout fields can all be affected, as can hidden fields and URL parameters.

Vulnerable pattern: input is pasted into the queryphp
<?php
$email = $_POST['email'];
$result = $db->query("SELECT * FROM leads WHERE email = '$email'");
// If email is: ' OR '1'='1
// the query now matches every row.

Why it costs you leads or sales

A successful attack can expose your leads and customer data, change or delete records, create admin accounts or let an attacker take over the site. That means lost lead data, broken forms, a site taken offline and possible legal duties if personal data leaks, such as breach notification under GDPR.

Search engines and browsers can also flag a hacked site with warnings, and ad platforms can suspend accounts that point to compromised pages. Visitors who see a warning do not fill in your form.

How an injection attack reaches your data
  1. Visitor submits formField contains SQL code
  2. Server builds queryInput pasted into SQL string
  3. Database runs itAttacker's command executes
  4. Data exposed or changedLeads, users, orders at risk

The break happens at the query step, where input is treated as code.

How to check if your site has it

Check a site for SQL injection
  1. List every input

    Write down each form, search box, login, filter and URL parameter (such as ?id=) that reaches your database.

  2. Look for database errors

    On staging, submit a single apostrophe (') in a text field. A raw SQL error message, a blank page or a 500 error suggests the input is not handled safely. A clean response does not prove you are safe.

  3. Run an automated scanner

    Use a web vulnerability scanner such as OWASP ZAP against staging. It tries many injection patterns that manual testing misses.

  4. Review the code

    Search custom code for queries built with string joining, such as concatenation or variables inside quotes. Each one is a suspect.

  5. Check your plugins

    Search the vulnerability databases for your plugins and themes by name and version. Many real-world injections come from outdated third-party code.

  6. Read the logs

    Look in server and web application firewall logs for requests containing strings like UNION SELECT, OR 1=1 or sleep(.

How to fix it

The core fix is the same everywhere: keep SQL code and user data separate. A prepared statement sends the query shape first and the values second, so the database never treats a value as a command.

Query handling

Vulnerable

  • Input joined into the SQL string
  • Database account can change or drop any table
  • Detailed SQL errors shown to visitors

Fixed

  • Prepared statements with bound values
  • Least-privilege database account
  • Generic error message, details only in logs
Fix by platform
  1. Update everything

    Update WordPress core, plugins and themes under Dashboard > Updates. Remove plugins you do not use or that are no longer maintained.

  2. Use $wpdb->prepare in custom code

    Replace any query built with string joining by $wpdb->prepare() with %s and %d placeholders. See the code example below.

  3. Report plugin flaws

    If a plugin is vulnerable and has no patch, replace it and tell the author through the plugin's support page.

Safe version with PDO prepared statementsphp
<?php
$stmt = $pdo->prepare('SELECT * FROM leads WHERE email = :email');
$stmt->execute([':email' => $_POST['email']]);
$rows = $stmt->fetchAll();
WordPress: $wpdb->preparephp
<?php
global $wpdb;
$email = sanitize_email( $_POST['email'] ?? '' );
$row = $wpdb->get_row(
  $wpdb->prepare(
    "SELECT * FROM {$wpdb->prefix}leads WHERE email = %s",
    $email
  )
);

How to confirm it's fixed

Confirmation checklist
  • Every query that uses form or URL input now uses prepared statements
  • A single apostrophe in each field no longer produces a database error
  • A rescan of staging with your vulnerability scanner shows no injection findings
  • Production shows a generic error page, not SQL details
  • The database user has only the permissions it needs
  • Plugins, themes and core are on current versions
  • Normal submissions still save correctly, including names like O'Brien

How to prevent it happening again

Ongoing habits
  • Make prepared statements the rule in code review
  • Update core, plugins and themes on a schedule
  • Remove abandoned plugins
  • Run a vulnerability scan after each major release
  • Schedule a penetration test for sites that hold sensitive data
  • Keep tested, offsite backups so you can recover
  • Review web server and WAF logs for injection patterns

How FunnelGuard handles this

This issue is outside what FunnelGuard checks. FunnelGuard is a synthetic monitoring service: a robot visitor runs your real customer journeys, such as lead forms and checkouts, and checks that steps, tracking and lead delivery work. It does not probe your forms for injection flaws.

Questions people ask

What is SQL injection in a form?

SQL injection is an attack where someone types database commands into a form field and the website runs them as part of a query. It happens when the site builds SQL by joining raw input into a string. It can expose, change or delete stored data.

How do I prevent SQL injection in a contact form?

Use prepared statements (parameterized queries) for every database query that includes form input. Add server-side validation, use a database account with minimal permissions and hide detailed error messages. Keep your CMS and plugins updated.

How can I test my website for SQL injection?

On a staging copy you own, submit an apostrophe in each field and look for database errors, then run a scanner such as OWASP ZAP. For sensitive sites, hire a security professional for a penetration test. Never test sites you do not have permission to test.

Is WordPress vulnerable to SQL injection?

WordPress core is regularly patched, but outdated or poorly written plugins and themes are a common source of SQL injection. Keep everything updated, delete unused plugins and use $wpdb->prepare() in any custom code.

Does input validation stop SQL injection?

Not on its own. Validation helps by rejecting unexpected input, but the reliable defence is prepared statements, which keep data and SQL code separate. Use both.

Does a web application firewall fix SQL injection?

A WAF can block many common attack patterns, but it does not remove the vulnerability. Fix the code with parameterized queries and treat the WAF as an extra layer.

Can FunnelGuard detect SQL injection?

No. FunnelGuard checks that your funnels, tracking and lead delivery work, not for security flaws. Use a vulnerability scanner, WAF or penetration test for SQL injection.

Does FunnelGuard catch this?

Other tool

Needs a different kind of tool.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.