Avoid concatenating untrusted values
// Unsafe: do not build SQL this way.
$sql = "SELECT * FROM users WHERE email = '" . $_GET['email'] . "'";
Use prepared statements
$stmt = $pdo->prepare(
'SELECT id, email FROM users WHERE email = :email'
);
$stmt->execute([
'email' => trim($_GET['email'] ?? ''),
]);
$user = $stmt->fetch();
The parameter is treated as data rather than SQL syntax.
Allowlist dynamic identifiers
Placeholders cannot represent column names or SQL keywords. Select dynamic sort columns from a strict server-side allowlist:
$allowed = ['title', 'created_at'];
$sort = $_GET['sort'] ?? 'created_at';
if (!in_array($sort, $allowed, true)) {
$sort = 'created_at';
}
$sql = "SELECT id, title FROM articles ORDER BY {$sort} DESC";
Defense in depth
Give the application database account only the permissions it needs. Avoid exposing database errors to visitors and review every query that includes user-controlled input.