'Invalid input.']); exit; } $type = $data['type']; $order = $data['order']; if (!is_array($order)) { http_response_code(400); echo json_encode(['error' => 'Order data must be an array.']); exit; } $table = ''; if ($type === 'category') { $table = 'categories'; } elseif ($type === 'bookmark') { $table = 'bookmarks'; } else { http_response_code(400); echo json_encode(['error' => 'Invalid type specified.']); exit; } try { $pdo->beginTransaction(); $stmt = $pdo->prepare("UPDATE {$table} SET sort_order = ? WHERE id = ?"); foreach ($order as $index => $id) { if (!is_numeric($id)) continue; // Basic sanitization $stmt->execute([$index + 1, $id]); } $pdo->commit(); echo json_encode(['success' => true]); } catch (Exception $e) { $pdo->rollBack(); http_response_code(500); echo json_encode(['error' => 'Database update failed.', 'details' => $e->getMessage()]); }