It could be due to a faulty cable, dirty charging port, or a problem with the charger or iPhone itself. Try using a different cable and charger, and clean the port carefully. If it still doesn’t work, it might need professional repair.
It may be because of a bad cable, greasy charging terminal or a problem with software. You can also use another charger, clean the port, or restart your iPhone and get an idea whether it works.