As for the prophet it was only his body that was put under a spill. and it was only Allah that could dispel the magic through Gabriel. Magics can never influence the thoughts of Prophet Muhammad and the other infallible.
and the reason why the body of a prophet can be put under a spell is because the prophet's body is basically similar to that of other people. it is his soul that is entirely purified from satanic influences and protected by Allah.