'ignored', 'name' => 'Already Logged', 'time' => date('H:i:s')]); exit; } } file_put_contents($debounce_file, $current_time); // ---------------------------------- try { $db = new PDO("mysql:host=db;dbname=PiClock", "root", "rootpassword"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // --- AUTOMATIC TABLE CREATION --- $db->exec("CREATE TABLE IF NOT EXISTS names ( id INT AUTO_INCREMENT PRIMARY KEY, CardID VARCHAR(12) NOT NULL UNIQUE, name VARCHAR(100) DEFAULT NULL )"); $db->exec("CREATE TABLE IF NOT EXISTS times ( UID VARCHAR(12) NOT NULL, temp FLOAT, timestamp DATETIME )"); // -------------------------------- // 1. Check if the card already exists in the names table $stmt = $db->prepare("SELECT name FROM names WHERE CardID = ?"); $stmt->execute([$clean_uid]); $user = $stmt->fetch(PDO::FETCH_ASSOC); if ($user) { // Card is known, grab the existing name $name = $user['name']; } else { // Card is UNKNOWN. Auto-register it. $maxStmt = $db->query("SELECT MAX(id) FROM names"); $max_id = $maxStmt->fetchColumn(); // If the table is empty, start at 1, otherwise use the highest + 1 $next_id = ($max_id !== null) ? ($max_id + 1) : 1; // Format the name to zz_UNASSIGNED-XX $name = sprintf("zz_UNASSIGNED-%02d", $next_id); // Insert the new card into the database $insertStmt = $db->prepare("INSERT INTO names (CardID, name) VALUES (?, ?)"); $insertStmt->execute([$clean_uid, $name]); } // 2. Log the swipe in the times table $stmt = $db->prepare("INSERT INTO times (UID, temp, timestamp) VALUES (?, 56.0, ?)"); $stmt->execute([$clean_uid, date('Y-m-d H:i:s')]); // 3. Send the result back to the front-end header('Content-Type: application/json'); echo json_encode(['status' => 'success', 'name' => $name, 'time' => date('H:i:s')]); } catch (PDOException $e) { file_put_contents($log_file, "DB ERROR: " . $e->getMessage() . PHP_EOL, FILE_APPEND); http_response_code(500); } } ?>