Personal Catalogue: PHP & MySQL — Chapter 5, Exercise 2 ==================================================== TASK Add the pre-checked checkboxes and the sync logic (including the else branch) to edit_item.php. Edit an existing book to remove one of its two tags and add a third, then confirm item_tags reflects exactly the new set — the removed tag's row gone, the third tag's row added, and the untouched tag's row unchanged. SOLUTION Starting from "The Hobbit" (id, say, 4) with two tags attached — Fantasy and Classic Fiction, per Exercise 1 — open edit_item.php?id=4. The Fantasy and Classic Fiction checkboxes should already be checked on page load (proof $itemTagIds and the in_array() check are wired correctly). Uncheck "Classic Fiction," leave "Fantasy" checked, and type a third, new tag into the "New tag(s)" field: New tag(s): Adventure Submit the form. The POST branch runs sync_item_tags($pdo, 4, $tagIds) where $tagIds now contains [Fantasy's id, Adventure's newly-created id] — Classic Fiction's id is genuinely absent from the submitted array, since its checkbox was unchecked. Confirm at the mysql> prompt: SELECT tags.name FROM tags JOIN item_tags ON item_tags.tag_id = tags.id WHERE item_tags.item_id = 4 ORDER BY tags.name; Expected output: +------------+ | name | +------------+ | Adventure | | Fantasy | +------------+ Classic Fiction is gone (removed via sync_item_tags()'s own $toRemove diff), Fantasy is still present and untouched (it was never in $toRemove or $toAdd, so no DELETE or INSERT touched its row at all), and Adventure is newly present (added via $toAdd after find_or_create_tag() created it). WHY THIS WORKS AS AN ANSWER ---------------------------- It exercises all three real outcomes sync_item_tags() is meant to produce in one pass — a genuine removal, a genuine addition via a brand-new tag, and an untouched tag left alone — and verifies the final state directly against the database rather than only trusting the form's own checkbox state after reload.