DEV_TIMEOUT_SECONDS)) { unset($_SESSION['DEV_PASSWORD_authenticated']); unset($_SESSION['DEV_last_activity']); header("Location: " . $_SERVER['PHP_SELF'] . "?reason=timeout"); exit; } $_SESSION['DEV_last_activity'] = time(); } if (isset($_GET['reason']) && $_GET['reason'] === 'timeout') { unset($_SESSION['DEV_PASSWORD_authenticated']); unset($_SESSION['DEV_last_activity']); } // Display Login Screen if not authenticated if (!isset($_SESSION['DEV_PASSWORD_authenticated']) || $_SESSION['DEV_PASSWORD_authenticated'] !== true) { $display_msg = "Developer Access"; if (isset($_GET['reason']) && $_GET['reason'] === 'timeout') { $login_error = "Logged out due to 30 min of inactivity."; } ?> Developer Login PiClock

PDO::ERRMODE_EXCEPTION]); } catch (PDOException $e) { die("Database connection failed: " . $e->getMessage()); } $message = ""; $current_table = (isset($_GET['table']) && $_GET['table'] === 'times') ? 'times' : 'names'; // Identify PK: 'UID' for times, 'id' for everything else $pk = ($current_table === 'times') ? 'UID' : 'id'; // --- HANDLE POST ACTIONS --- if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { if (isset($_POST['backup_all'])) { $pdo->exec("DROP TABLE IF EXISTS names_backup; CREATE TABLE names_backup AS SELECT * FROM names;"); $pdo->exec("DROP TABLE IF EXISTS times_backup; CREATE TABLE times_backup AS SELECT * FROM times;"); $message = "
Full Database Backup created!
"; } elseif (isset($_POST['restore_names'])) { try { // Ensure table exists first $pdo->exec("CREATE TABLE IF NOT EXISTS names ( id INT AUTO_INCREMENT PRIMARY KEY, CardID VARCHAR(12) NOT NULL UNIQUE, name VARCHAR(100) DEFAULT NULL )"); // Now restore $pdo->exec("TRUNCATE TABLE names; INSERT INTO names SELECT * FROM names_backup;"); $message = "
'names' table restored and re-created!
"; } catch (Exception $e) { $message = "
Error restoring 'names': " . $e->getMessage() . "
"; } } elseif (isset($_POST['restore_times'])) { try { // Ensure table exists first $pdo->exec("CREATE TABLE IF NOT EXISTS times ( UID VARCHAR(12) NOT NULL, temp FLOAT, timestamp DATETIME )"); // Now restore $pdo->exec("TRUNCATE TABLE times; INSERT INTO times SELECT * FROM times_backup;"); $message = "
'times' table restored and re-created!
"; } catch (Exception $e) { $message = "
Error restoring 'times': " . $e->getMessage() . "
"; } } elseif (isset($_POST['action']) && $_POST['action'] === 'save_changes') { // ONLY proceed if 'rows' exists and is an array if (isset($_POST['rows']) && is_array($_POST['rows'])) { $pdo->beginTransaction(); foreach ($_POST['rows'] as $pk_val => $columns) { $setClauses = []; $params = []; foreach ($columns as $col => $val) { $setClauses[] = "`$col` = ?"; $params[] = trim($val); } $params[] = $pk_val; $stmt = $pdo->prepare("UPDATE `$current_table` SET " . implode(", ", $setClauses) . " WHERE `$pk` = ?"); $stmt->execute($params); } $pdo->commit(); $message = "
Changes saved successfully!
"; } } elseif (isset($_POST['action']) && $_POST['action'] === 'delete_selected') { if (!empty($_POST['selected_ids'])) { $ids = $_POST['selected_ids']; $placeholders = implode(',', array_fill(0, count($ids), '?')); $stmt = $pdo->prepare("DELETE FROM `$current_table` WHERE `$pk` IN ($placeholders)"); $stmt->execute($ids); $message = "
Rows deleted successfully!
"; } } } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $message = "
Error: " . $e->getMessage() . "
"; } } // --- FETCH DATA --- $rows = []; $columns = []; $total_rows = 0; $total_pages = 0; // Only attempt to fetch data if the table actually exists try { // Check if table exists by trying to select one row $pdo->query("SELECT 1 FROM `$current_table` LIMIT 1"); // If we reach here, table exists, proceed with standard queries $colStmt = $pdo->query("SHOW COLUMNS FROM `$current_table`"); $columns = $colStmt->fetchAll(PDO::FETCH_COLUMN); $sort_col = (isset($_GET['sort']) && in_array($_GET['sort'], $columns)) ? $_GET['sort'] : $pk; $sort_dir = (isset($_GET['dir']) && in_array($_GET['dir'], ['ASC', 'DESC'])) ? $_GET['dir'] : 'ASC'; $page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1; $limit = 50; $offset = ($page - 1) * $limit; $countStmt = $pdo->query("SELECT COUNT(*) FROM `$current_table`"); $total_rows = $countStmt->fetchColumn(); $total_pages = ceil($total_rows / $limit); $stmt = $pdo->prepare("SELECT * FROM `$current_table` ORDER BY `$sort_col` $sort_dir LIMIT :limit OFFSET :offset"); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); $stmt->bindValue(':offset', $offset, PDO::PARAM_INT); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { // Table doesn't exist, leave $rows, $columns, etc., as empty arrays // The page will render, but the table section will be empty/blank } function buildUrl($params = []) { return "?" . http_build_query(array_merge($_GET, $params)); } ?> PiClock DB Management

🛠️ Developer DB Management

Log Out
Names Table Times Table
$col, 'dir' => $next_dir]); ?> No columns found (Table empty)