Consider
$$x=\sin(\phi)$$
such that
$$dx=\cos(\phi)d\phi$$
and it follows that
$$\frac{\partial\phi}{\partial x}=\frac{1}{\cos(\phi)}$$
Now, on the right hand side we could take the derivative with respect to $\phi$ and proceed as
$$\frac{\partial}{\partial\phi}\frac{1}{\cos(\phi)}=\frac{\sin(\phi)}{\cos^2(\phi)}$$
while on the left hand side we trivially obtain:
$$\frac{\partial}{\partial\phi}\left(\frac{\partial\phi}{\partial x}\right)=\frac{\partial^2\phi}{\partial\phi\partial x}=\frac{\partial}{\partial x}\left(\frac{\partial\phi}{\partial \phi}\right)=\left(\frac{\partial}{\partial x} 1\right)=0$$
Clearly,
$$0=\frac{\sin(\phi)}{\cos^2(\phi)}$$
is not valid in general. Therefore, my question: what went wrong and how to fix it?
The partial derivatives with respect to $\phi$ and $x$ don't commute since $\phi$ is a function of $x$. It would be like saying $\frac{\partial}{\partial x^2}\frac{\partial}{\partial x}x^2=0$ since $\frac{\partial}{\partial x^2}x^2=1$, but actually it is $\frac{1}{x}$.