
Mini lesson · English
How to save form submissions in a database
Save a website form in a supplied practice database project, then verify the record survives reopening the page.
Who is this for?
People who want website submissions to be saved. A coding assistant can apply the provided setup and connect the form; writing SQL or server code is not required.
Before you start
Your own disposable Supabase project and Netlify-function practice project. Use the complete supplied files and a coding assistant for setup. Keep private database credentials on the server and test only synthetic messages. Worked answers are illustrative examples, not completed tests on your account. Replace their sample observations with your own results, or identify the step that is still blocked.
Estimated time: 20–37 min
Estimates for the supplied starter files with prerequisite accounts and tools ready. Setup, deployment queues and delivery/DNS waits take additional time. Account setup is additional. The restricted lab is not a public anonymous feedback service; review durable abuse limits before adapting it for public use.
What you will learn
- Send a form record to real storage.
- Verify persistence in the provider’s table view.
- Reject invalid input without exposing everyone’s records.
Step 1
Define what saving means
The record must exist outside the current browser page.
Open the ready form with your assistant and choose a distinctive synthetic message. We will save it through the provided server into a separate practice database.
A value still shown on the page is not proof of database storage. Plan to find the same message in the provider and after reopening.
A concrete example
The provider table and the current browser page are different places. A message that remains in the table after closing the app demonstrates more than a sentence still displayed beside the submit button.
Use the supplied feedback-storage project. Help apply its provided schema only to my separate Supabase practice database and configure the private server environment. Keep visitor access to the feedback table restricted. Start the form, guide me to the provider table view and help check that valid data persists while invalid requests create no row. Explain my checks, not SQL syntax.Files for this step
Open or download the files below. When saving a project, keep the folder paths shown in the filenames.
How it works (optional)
Store a record outside the browser
A saved database row should still exist after you close the page.
A JavaScript variable disappears when its page closes. Browser local storage stays on that browser. A database stores records on a service that your application can access under explicit permissions.
We will use a private Supabase table and a server function. The browser sends a small message; the server validates it and writes the row. This lesson does not expose a public endpoint that lists everyone’s messages.
Worked example
After sending “Database test D1”, close the page and find that exact message in your project’s table editor. A message displayed only in the page is not storage evidence.
Extra implementation exercise
Your page shows the submitted message from a local variable. Does that prove the database saved it?
Show a worked solution
No. Verify the actual row in the database. The browser could display the input even if no server request happened.
- Separates displayed state from storage.
- Checks the matching row through an authorized database view.
Common mistake
Equating a page’s success text with durable storage.
Step 2
Prepare the isolated storage project
Have the assistant apply the supplied setup safely.
Create or choose your disposable Supabase project. Ask the assistant to review and help apply schema.sql there, configure the private values and run the complete project using run-locally.txt.
Confirm the correct project before applying changes. Keep public visitors unable to browse all feedback; use the owner’s Table Editor to inspect the test row.
A concrete example
The supplied feedback table has a message, a generated ID and a creation time. The project owner can inspect it in the provider; visitors do not need permission to read everyone’s feedback for this test to work.
Supabase: row-level security · Node.js: supported release status
Try it yourself
Which practice project did you prepare, and can you identify its owner table view?
Discuss your answer with the lesson tutor
Show a worked solution
Example setup record: The assistant applied schema.sql only in my disposable practice project, configured the private server values and started the supplied app. I can inspect feedback as the project owner in Table Editor. Public visitors still cannot browse that table.
- Identifies the disposable project and completed or blocked supplied-schema/server setup.
- Locates the owner’s feedback Table Editor without opening visitor read access.
Files for this step
Open or download the files below. When saving a project, keep the folder paths shown in the filenames.
How it works (optional)
Create a private practice table
Enable row-level security and keep direct browser access closed in this worked route.
Run this schema only in the isolated practice Supabase project named in your setup. Browser access is intentionally restricted; verify the row as the project owner in Dashboard → Table Editor. Do not run this lab schema against a production database.
Create a separate Supabase practice project and run schema.sql in its SQL editor. The table has a generated ID, a constrained message and a server timestamp. Do not run sample schema changes against an unrelated production database.
The script enables row-level security and removes table privileges from browser roles. The server’s secret/service-role key can perform the privileged write; it must remain server-side. A publishable browser key is different and does not grant these administrative permissions.
Open Table Editor in the Supabase project dashboard to inspect your practice row; this is not the browser's developer console. For a failed insert, inspect the server response and the project's database/schema configuration. Do not enable anonymous SELECT merely to see whether an insert worked.
For the complete local sequence, open run-locally.txt: prepare the private root environment, link only the practice project and start the Netlify function proxy. server-env.example.txt lists variable names without real secrets. Opening only the HTML cannot run the server.
Worked example
No broad “allow all” policy is needed for this server-mediated lab. Opening anonymous SELECT to diagnose an error would also expose submitted messages.
create table public.feedback (
id uuid primary key default gen_random_uuid(),
message text not null check (char_length(message) between 1 and 1000),
created_at timestamptz not null default now()
);
alter table public.feedback enable row level security;
revoke all on public.feedback from anon, authenticated;
grant insert on public.feedback to service_role;
-- No browser read/write policies: the practice server uses its secret key.
Supabase: row-level security · Node.js: supported release status
Extra implementation exercise
A browser query is denied. Should you fix it by allowing anonymous users to read the whole feedback table?
Show a worked solution
No. This design intentionally denies browser reads. Check the server’s write setup and inspect the table as its owner.
- Preserves the intended private-data boundary.
- Uses the server route or authorized dashboard rather than opening global reads.
Common mistake
Disabling security to make an error disappear without considering who gains access.
Step 3
Submit a record from the page
Let the ready server handle the write.
Open the real local or hosted practice app and submit a message such as D1. Ask your assistant to keep only intended visitor fields in the saved record.
Find the exact D1 text in the provider table and inspect its generated identity/time. Keep secret account values out of screenshots or shared notes.
A concrete example
Submit D1, then find that exact text in feedback. The generated ID distinguishes this row from an old message with similar wording; the visitor should not choose or overwrite that identity.
Try it yourself
What appeared in the form and in the provider for D1?
Discuss your answer with the lesson tutor
Show a worked solution
Example save record: The form reported a saved message. In the practice provider’s feedback table I found “D1 — blue notebook” with a generated ID and creation time. I kept that ID in my private test notes so I could identify the same row later.
- Matches the exact submitted D1 text to a provider row with generated identity/time.
- Reports page and storage results separately, without sharing private configuration.
Files for this step
Open or download the files below. When saving a project, keep the folder paths shown in the filenames.
How it works (optional)
Connect a validated server write
The server chooses the table and permitted fields; the caller supplies only the message.
Configure the three named server variables and deploy or run the separate function project. The tester enters the practice access code. It authorizes this restricted endpoint, not the Supabase account.
Read mini-feedback.mjs: the function checks method, access code, size and message before calling the provider. It ignores caller-supplied IDs or table names and returns a storage success only after a successful insert response.
Worked example
A request containing an extra admin field does not become an administrative update: the server constructs a new object containing only message.
Extra implementation exercise
Why construct {message} instead of forwarding the entire browser JSON object to the database?
Show a worked solution
It keeps the server’s accepted schema explicit and prevents unexpected fields from becoming writes. Validate the message too; selecting fields alone is not sufficient.
- Limits the writable fields.
- Does not trust client-controlled IDs, timestamps or privileges.
Common mistake
Letting the browser choose an arbitrary table or privileged column.
Step 4
Check persistence and failure
Repeat the action after closing the first page.
Close and reopen the app, then verify D1 still exists in storage. Try missing/oversized input and a wrong practice code in the disposable setup.
Invalid attempts must not create new rows. A previous success message must not remain as if a later failed attempt also saved.
A concrete example
Record the valid row before the negative tests, then inspect the table afterward. “The form showed an error” and “no invalid row was added” are two useful parts of the check.
Try it yourself
Which records remained after reopening, and what happened for your invalid attempts?
Discuss your answer with the lesson tutor
Show a worked solution
Example negative-test record: After closing and reopening the page, D1 remained in the provider table. Empty and oversized attempts, plus a wrong practice code, produced no new rows. The failed attempt showed an error rather than leaving the earlier Saved message visible as if it applied again.
- Checks that the identified valid record remains after reopening.
- Verifies that invalid input/access attempts add no rows and do not retain misleading success state.
Files for this step
Open or download the files below. When saving a project, keep the folder paths shown in the filenames.
How it works (optional)
Verify persistence and failures
Use distinctive records and inspect the database independently of the page.
Send D1 with the correct practice code and inspect its matching row in the table editor. Close and reopen the page, then confirm the row still exists. Record the row’s ID and timestamp without sharing account secrets.
Try a missing message, an oversized message and an incorrect access code. None should create a new row. In a disposable setup, check that missing server configuration produces an honest failure.
Worked example
A valid record plus two rejected invalid requests gives more information than three repeated successful clicks. Avoid using real personal feedback for a first lab.
Extra implementation exercise
The endpoint returns an error, but the browser still says “Saved” from a previous attempt. What should you verify?
Show a worked solution
Check whether the new value has a row. The UI should show the current failure, and an earlier success must not be counted as this attempt’s result.
- Checks the current response and a distinctive new test value.
- Does not reuse an old success message as evidence for a failed request.
Common mistake
Testing several requests with indistinguishable messages.
Step 5
Add a topic without opening the data
Use an assistant for the field change and inspect the result.
Ask the assistant to add two topics, Project and Question, to the form, storage setup and server validation. Their stored values are project and question. The supplied reference is a comparison to open after your own attempt, not the place you must discover the requirement.
Create one controlled record per topic and try an unsupported topic, other, with the assistant’s help. Verify both saved values and the rejection, keeping the feedback table private to visitors.
A concrete example
Add two allowed topics, Project and Question. Each chosen value should be stored beside its message. Other is a rejection test, so an attractive selector alone does not complete the storage change.
Try it yourself
Start with these materials
The starter files and test content are supplied. A live verification still requires your own permitted provider account, inbox or domain as listed under prerequisites. Local inspection is possible without them; record the live step as not tested until you actually perform it.
Provide the topic change and observed valid/invalid storage results.
What to produce A redacted storage record showing valid persistence and rejected invalid attempts.
Discuss your answer with the lesson tutor
Show a worked solution
Example extension record: “D2 — project test” persisted with topic project; “D3 — question test” persisted with topic question. The unsupported value other was rejected and created no new row. I inspected these as the owner; visitor read access to the full table remained restricted.
- Saves Project and Question topic values with their matching controlled records.
- Rejects an unsupported topic while keeping visitors unable to browse the feedback table.
Files for this step
Open or download the files below. When saving a project, keep the folder paths shown in the filenames.
- schema.sqlDownload this file
- netlify/functions/mini-feedback.mjsDownload this file
- storage-record.txtDownload this file
- worksheet.htmlDownload this file
- reference/assignment-walkthrough.txtDownload this file
- reference/START-HERE.txtDownload this file
- reference/public/index.htmlDownload this file
- reference/public/app.jsDownload this file
- reference/netlify/functions/mini-feedback.mjsDownload this file
- reference/add-topic.sqlDownload this file
How it works (optional)
Your assignment: store a validated topic
Extend the data model and server together without opening browser permissions.
Add a topic column constrained to project or question. Update the server to validate the allowlist and construct the inserted object explicitly. Add the corresponding form control.
Create one record per allowed topic and reject an unsupported value. Keep generated IDs and timestamps under database control.
DEFAULT assigns question to existing rows in this exercise. Without a value/default, adding a NOT NULL column can fail when rows already exist. In a real migration, choose the backfill value according to the data rather than copying this lab default.
Worked example
The database constraint backs up the server validation. Neither replaces the other’s role in clear errors and data integrity.
The complete reference walkthrough is available in reference/assignment-walkthrough.txt. Compare its steps with your own result; fill the test record from observations in your own project.
-- Existing rows need a value when the new column is NOT NULL.
alter table public.feedback add column topic text not null default 'question';
alter table public.feedback add constraint feedback_topic_allowed
check (topic in ('project','question'));Extra implementation exercise
Add the topic field and prove that valid data persists while invalid data is rejected.
Show a worked solution
The topic extension requires all three layers to agree: a browser select, the server allowlist and a database column/check constraint. The reference ALTER TABLE handles the starter's existing rows by assigning the documented default topic before enforcing the constraint.
The server writes only {message,topic}. It must reject an unsupported topic before using the secret database credential. It must also ignore caller-supplied IDs/table names and report a provider write failure as failure.
Inspect the exact saved message/topic in Supabase's dashboard Table Editor, not a client-side success paragraph. Reload or close the browser and verify the same stored record. Use account-owned diagnostics; do not expose anonymous reads to troubleshoot.
- Topic validation exists in the server and database.
- Matching valid rows persist and unsupported values do not create rows.
Common mistake
Updating only the form while leaving the server and database unaware of the new field.
Was this lesson useful?
Leave a like, or share it with someone who could use it.
Continue learning
- How to build your first personal website
- How to publish your website from GitHub
- How to connect a custom domain to your website
- How to set up email on your own domain
- How to make your website usable on phones
- How to make website images load faster
- How to get website feedback in your inbox
- How to add email subscriptions to your website
- How to send automated emails from your website
- How to let people sign in to your website
- How to keep a page private
- How to let users edit their profile information
- How to let users upload images
- How to keep API keys out of your frontend
- How to show data from an external API on your website
- How to add an AI chatbot to your website
- How to make a chatbot answer from your own content
- How to add a blog to your website
- How to add search to your website
- How to add another language to your website
- How to help Google find your website
- How to fix your website preview when sharing a link
- How to measure visits and useful actions on your website
- How to let visitors book an appointment
- How to accept a payment on your website
- How to preview website changes before publishing
- How to undo a broken website deployment
- How to back up and restore your website data
- How to find and fix errors on your live website