Skip to main content

Posting Data

Kvery isn't read-only. A query can write to your database — insert, update or delete rows — and accept input through form variables. This turns a query into a small write endpoint or an editable grid.

Write queries​

Any data-modifying statement works, with values supplied as $-prefixed form variables:

INSERT INTO leads (name, email, source)
VALUES ('$name', '$email', '$source');
UPDATE tickets
SET status = '$status'
WHERE id = '$id';

Each $-prefixed variable becomes a form field (or an API parameter) that Kvery fills from the request when the query runs. Apostrophes in the values are escaped, so put text in quotes (see Form variables).

HTTP method matters for the API​

Kvery runs a query as a write query when its SQL contains INSERT, UPDATE, DELETE, REPLACE, ALTER or CREATE followed by a space — anywhere in the text, in any letter case. A write query takes its values from posted data: its run form posts them, and when the query is published as an API, callers send them with a POST, PUT, PATCH or DELETE request. The query-string values of a GET request are not passed to a write query (a request body, the path variables of a custom URL and the bearer token still are). A read query takes its values with any method.

Send the values in the request body as form fields (URL-encoded or multipart) or as a JSON object, whose fields become the values. On the query API a JSON list of objects runs the query once for every object (see Batch requests).

The share hash itself answers every one of these methods. To accept a single method only, choose it in the method selector in front of the Custom URL of the share dialog (see HTTP methods and write queries).

Editable tables from results​

Row-level actions (such as edit or delete) are columns of a results table that open a write query for their row, for example a Link form field placed in a column. Mark such a column with the operations:: column modifier so it is left out of sorting and searching. The modifier does not create the controls; the write query performs the change.

Controlling the response​

Pair write queries with directives to return a clean result:

INSERT INTO leads (email) VALUES ('$email')
ON ERROR RETURN 'That email could not be saved' SET HTTPCODE '422';

The directives go at the end of the statement, before its ;. For a write query on its own, the message and the status code are returned when the insert fails or changes no rows. When the query is written in statement blocks ({% … %}) and its last block starts with SELECT or EXEC, the run is in read mode: a write statement with ON ERROR RETURN then returns no rows, so it answers with the message even after a successful write.

Tips​

  • Validate critical inputs in SQL (constraints, WHERE guards) — don't rely on the form alone.
  • Use webhooks (ON SUCCESS POST) to notify other systems after a successful write.
  • For bulk writes via the API, see Batch requests.