prepare($query); $stmt->execute(['user_id' => $user_id]); $assigned_services = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $user_service_id = intval($_POST['user_service_id']); $service_state = $_POST['service_state']; // Update the last state in the database $update_query = "UPDATE user_services SET last_state = :last_state WHERE id = :id"; $stmt = $pdo->prepare($update_query); $stmt->execute(['last_state' => $service_state, 'id' => $user_service_id]); // Execute the appropriate code for the selected state $state_query = "SELECT state_on_code, state_off_code, state_auto_code FROM user_services WHERE id = :id"; $stmt = $pdo->prepare($state_query); $stmt->execute(['id' => $user_service_id]); $service = $stmt->fetch(); if ($service) { try { if ($service_state === 'on') { ob_start(); // Start output buffering eval($service['state_on_code']); ob_end_clean(); // Clean (erase) the output buffer and turn off output buffering } elseif ($service_state === 'off') { ob_start(); // Start output buffering eval($service['state_off_code']); ob_end_clean(); // Clean (erase) the output buffer and turn off output buffering } elseif ($service_state === 'auto') { ob_start(); // Start output buffering eval($service['state_auto_code']); ob_end_clean(); // Clean (erase) the output buffer and turn off output buffering } } catch (Throwable $e) { // Handle any errors that occur during eval() error_log("Error executing service code: " . $e->getMessage()); // Log the error for debugging } } } ?>