setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec('CREATE TABLE IF NOT EXISTS entries ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, msg TEXT NOT NULL, ts INTEGER NOT NULL )'); $isHtmx = isset($_SERVER['HTTP_HX_REQUEST']); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = trim($_POST['name'] ?? ''); $msg = trim($_POST['msg'] ?? ''); if ($name !== '' && $msg !== '') { $stmt = $db->prepare('INSERT INTO entries (name, msg, ts) VALUES (?, ?, ?)'); $stmt->execute([mb_substr($name, 0, 40), mb_substr($msg, 0, 500), time()]); } if (!$isHtmx) { header('Location: /guestbook.php'); exit; } } $rows = $db->query('SELECT name, msg, ts FROM entries ORDER BY id DESC LIMIT 50'); function render_entries(iterable $rows): void { foreach ($rows as $r) { ?>

guestbook