Parameterized vs concatenated queries
Build the same product search three ways and watch the token view: concatenation lets input change the query, bound parameters and allowlists keep it a value.
Goal
The product search on a shop sends three user inputs to the database: the search text, the sort column, and the page size. Today all three are glued into the SQL string.
For each input, choose how the server builds the query, type any value you like, and watch the token view. The query is safe when no input can change its shape. Nothing here runs against a real database.
SELECT id, name, price FROM products WHERE name LIKE '%O'Brien%'
Unterminated string starting at character 64
ORDER BY price
ORDERkeywordBYkeywordpriceidentifier
The input stayed a single value. The query shape matches the template.
LIMIT 25
LIMITkeyword25number
The input stayed a single value. The query shape matches the template.
At least one input can still change the query or silently break it.
Steps
01Pick a construction method for all three inputs so that no value, typed or not, can change the shape of the query.
Do this in the workbench above. This step completes itself when the condition is met.
02With string concatenation on the search box, which everyday input makes the query fail with a syntax error?
03Why is escaping quotes not a complete fix?
04What happens if the sort column is sent as a bound parameter?
05What does a bound parameter guarantee?