Toilet flange repair isn’t easy. There are a couple of possibilities, and everything depends on the size and type of pipe.
If the pipe has a 4-inch inside diameter:
If the plastic pipe has a 3-inch inside diameter, you’ll have to remove the old flange from the outside of the pipe. This is a difficult task because you have to make the opening around the flange large enough to peal the old flange off the pipe. In addition, there is no guarantee of success with this type of toilet repair; it takes a plumber many years to master the art of splitting the glue joint without damaging the pipe.
If a closet flange is cast iron, then you would have to use a hammer and chisel to break the flange and remove it.
If it is a 4-inch pipe:
If it is a 3-inch cast iron pipe:
If the closet flange is lead and installed on the outside of the pipe, you will have to remove the old flange and re-caulk it, and then pour a new lead joint. You can also install an expanding one on the outside, but that would require breaking up the floor to get the flange down into the floor.
After setting every flange, make sure you screw it to the floor so it will not rise up when you tighten down the toilet. Bolt the flange down through the holes supplied in the flange with concrete or wood anchors that are galvanized or brass so they won't rust and break. And when you tighten it, never use more than a 6-inch crescent and two fingers on the handle to get the nuts tight. In addition, if you use a plastic flange, make sure you use one with a stainless steel outer ring, so it won't rust and is stronger than the plastic itself. Again, no toilet should be tightened so tight it breaks the flange. A toilet should always be set level and the edge that touches the floor should be sealed with calk to prevent it from smelling.