Loose contact of charging connector or old lightning cable makes such a case in my experience. Check this first if same then you may replace the charging connector.
open