CiTi · Computer Coding · Year One · Unit 4
The index, add, and edit pattern behind every admin we build
What purposes do databases serve? What distinguishes relational databases?
Hands-On:
#4 Design a relational database
#5 Write and execute database queries
#7 Validate and debug my own work
Professional:
#7 Treat a mistake as data, not failure
#9 Document as you go
Following data from a form, through a server, into a file, and back onto a page. Keys that identify one record and point at another.
Reusing one pattern across many jobs, testing with awkward real data, and checking the file, not just the screen.
Building a list page, an add form that runs [append], and an edit form that prefills a record and runs [replace]: the three files every admin we build is made of.
Students will build a three-page admin for their state table, learning the index, add, and edit pattern used for every table in every admin we build.
[append] and why typed text is wrapped in [url].[replace] picks which record to change.The admin is a real piece of the Aimee Mohr site: a state added here shows up in the contact form menu from the last lesson.
amp folder with state.db, country.db, and the working contact menu from Y1-U4-L5./citi/coding/2027/ajm/amp/25admin25/state/ (behind the admin login).edit.html pointed at a throwaway copy of state.db./curriculum/js/codelock.js. It cannot be selected or copied, and the code text is not in the page source. The handout shows partial code with pink blanks.Every table gets one folder with three files. This is the routine for every admin we build, so name it out loud each time.
| File | Job | Key tag |
|---|---|---|
| index.html | Lists every record. No form: it links to add.html and edit.html | [search] |
| add.html | Blank form; saves a new record | [append] |
| edit.html?skuvar=1038 | Filled-in form; saves changes to one record | [replace] |
First years keep these separate: the list never holds a form. Its + Add a state link opens add.html, where the form lives. Both forms use action="[thisurl]", so each posts back to its own page with no file name typed. [thisurl] leaves off the ?skuvar= part, which is why edit needs the hidden input. The processing block sits at the top, right after vars.inc. A plain visit skips it and shows the form; a submit runs it, saves, and redirects to the list. Students use 27admin27/state/ in their amp folder.
Nothing new: it is the Search Lab loop from L5 with table rows. All 101 records list. An admin shows everything, inactive records included; the public page filters on status. The Country column uses [lookup]: find the row in country.db whose SKU matches this state’s R_COUNTRY and print its TITLE. Same foreign key as L5, one tag instead of a nested search.
Inputs use f_ names so they cannot collide with field tags. The hidden action input is what the processing block checks. The country option sends [SKU], because R_COUNTRY stores the country’s SKU.
| Piece | Job |
|---|---|
| [showif [action]=add] | Only runs when the form was sent. A plain visit skips it and shows the form. |
| [append db=...] | Adds one new line (record) to the end of the database. |
| autonumber=SKU | WebDNA fills in the SKU: the highest one so far, plus 1. |
| FIELD=value | One pair per field. The name must match the .hdr exactly. |
| & | Goes between pairs. This is how WebDNA knows where one value ends. |
| [url]...[/url] | Wraps anything a person typed, so an & in it cannot cut the value off. |
| [!] [/!] | Hides the line returns, so each pair can sit on its own line. |
| STATUS=[f_status] | From the status menu. Menu values are never typed, so no [url]. |
| [closedatabase] | Saves to the file now. The footer never runs, because... |
| [redirect index.html] | ...the browser is sent back to the list, which shows the new record. |
[closedatabase] is here because [redirect] ends the page, so the footer’s close never runs.
Add the Edit column to the index: <td><a href="edit.html?skuvar=[SKU]">Edit</a></td>. The SKU rides in the URL as skuvar. Never name it sku: it would collide with the [SKU] field tag inside a search.
The whole form sits inside a search for that one SKU, so every field tag is that record. [input] makes the value safe inside quotes. The country select runs a second search, and inside it [SKU] means the country, so the state’s R_COUNTRY is saved into [statecountry] first: the same trick as [countrysku] in L5.
| [append] (add.html) | [replace] (edit.html) | |
|---|---|---|
| Does what | Adds a new record | Changes an existing record |
| Which record | A new one at the end | Every record that matches its search |
| SKU | autonumber=SKU makes one | eqSKUdatarq=[skuvar] finds one |
| STATUS | From the menu, Active first | From the menu, prefilled with selected |
Saving Trinidad & Tobago without [url] stored only Trinidad . With it, the full name was saved.
Without the & after the name, TITLE was saved as No AmpCODE=NM and CODE was blank.
TITEL= in the append saved the record with an empty name and no error.
Without autonumber=SKU the record was saved with an empty SKU. Its Edit link is skuvar= and opens a page with no form.
Without action=add the form just reloads empty. Nothing is saved and there is no error.
The edit page redirected as if it had saved, but no record changed.
A name with " in it printed as value="Trinidad & "Tobago"": the box shows only Trinidad &, and the next save loses the rest.
Without the [showif], no option is marked selected, so every state’s menu opens on Canada, the first option. One careless save moves New York to Canada.
With eqR_COUNTRYdatarq=1000 in place of the SKU search, one save rewrote every US record as Alaska2: all 56 in the full table. Demo this on a copy, never on student data.
In our test the append still worked with real returns in place of [!] [/!]. Teach the comment as the house style that keeps the pairs readable, not as a fix.
state.db in CyberDuck after each test..hdr. Form names (f_title) and field names (TITLE) are different on purpose.header.inc.[showif], [search], and [founditems] closes. On edit.html there are two searches; indent and count.| Time | Phase | What happens |
|---|---|---|
| Day 1: index and add | ||
| 5 min | Hook | On the teacher admin, add a made-up state. Open the AMP contact form: it is in the menu. “No one opened the database file.” |
| 5 min | The Pattern | Draw the three files and the arrows: list to add, add saves and returns to list. |
| 15 min | Part A | Make the folder and index.html. All 101 records should list. |
| 10 min | Part B | Build the add form. Submit it now: nothing saves yet. Ask why. |
| 15 min | Part C | Take the append apart piece by piece, add it, and add Trinidad & Tobago. Open state.db to see the new line. |
| 5 min | Break It | Pairs run experiments 1 and 2. The rest are for early finishers. |
| Day 2: edit and replace | ||
| 5 min | Part D | Add the Edit links. Hover them: each URL carries a different SKU. |
| 20 min | Part E | Build the edit form and check that every field fills in, the country and the status especially. |
| 15 min | Part F | Add the replace and fill in the comparison table together. Then the teacher demo: a wrong criterion on a copy, and every US state changes. |
| 10 min | Break It | Experiments 6 and 7, then the rest. |
| 5 min | Exit Ticket | Check-in and exit ticket. |
Formative: live check of 27admin27/state/: the index lists all records with country names; adding Trinidad & Tobago saves the full name; editing a state prefills every field, the country and status included, and the save shows on the index and in the contact menu.
Exit ticket (completion-graded): what [url] protects, and how [replace] knows which record to change.
Extension: show Active or Inactive on the index instead of 1 or 0, and make the record count say 1 record when there is only one.