The first thing you should do is clean the iPhone's USB-C port with something dry and gentle, like a toothpick or a very soft brush. Sometimes a lot of dust and lint builds up inside without you noticing, and that causes exactly what you're describing: the phone charging intermittently or not recognizing the cable. I had the exact same drama with my phone once and it turned out to be just that. Do it carefully so you don't scratch anything inside.
If you clean the port and it still doesn't work, then it could be a hardware problem. But before you think about damage, try cables that you know work perfectly on other devices. The cable that came in the box isn't always in perfect condition, and the Anker one you use on your iPad could be damaged or have some specific issue. Try using a different cable on the iPhone and vice versa to figure out if the problem is the cable or the port.
If none of that works after cleaning it thoroughly, then it really sounds like the port got damaged somehow. In that case you'd have to contact Apple or an authorized service center because it's a hardware component. But honestly, 80% of the time something like this happens it ends up being dust inside or a cable with some specific problem, so don't panic yet.