Thanks for writing to health care magic.
There could be a anal fissure
which is resulting in pain while passing stool.
When a child is constipated and passes a hard stool it could result in the rupture of the anal mucosa resulting in fissure.
So to prevent constipation
give your kid a lot of fresh fruits, green leafy vegetables and plenty of water.
Avoid junk food.
Consult your doctor who may do a local examination to identify the cause.
Hope this helps.