query(" SELECT p.name, SUM(si.quantity) as total_quantity FROM sale_items si JOIN products p ON si.product_id = p.id GROUP BY p.name ORDER BY total_quantity DESC LIMIT 5 "); $best_selling_products = $stmt->fetchAll(PDO::FETCH_ASSOC); header('Content-Type: application/json'); echo json_encode($best_selling_products); } catch (PDOException $e) { http_response_code(500); echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); } ?>