Consider the finite square well, where we take the potential to be $$V(x)=\begin{cases} -V_0 & \text{for}\,\, |x| \le a \\ \,\,\,\,\,0 & \text{for}\,\, |x|\gt a \end{cases}$$ for a positive constant $V_0$.
Within the square well the time-independent Schr${ö}$dinger equation has the form $$-\frac{\hbar^2}{2m}\frac{d^2 u}{dx^2}=(E-V)u=(E+V_0)u\tag{1}$$
While outside the square well the equation is $$-\frac{\hbar^2}{2m}\frac{d^2 u}{dx^2}=Eu\tag{2}$$ with $E$ being the total energy of the wavefunction $u$ where $u=u(x)$.
The graph of the potential function is shown below:
Rearranging $(1)$ I find that $$\frac{d^2 u}{dx^2}=-\underbrace{\bbox[#FFA]{\frac{2m}{\hbar^2}(E+V_0)}}_{\bbox[#FFA]{=k^2}}u$$ $$\implies \frac{d^2 u}{dx^2}+k^2u=0\tag{3}$$ with $$k=\frac{\sqrt{2m(E+V_0)}}{\hbar}\tag{A}$$
So equation $(3)$ implies that there will be oscillatory solutions (sines/cosines) within the well.
Rearranging $(2)$ I find that $$\frac{d^2 u}{dx^2}=-\underbrace{\bbox[#AFA]{\frac{2m}{\hbar^2}E}}_{\bbox[#AFA]{=\gamma^2}}u$$ $$\implies\frac{d^2 u}{dx^2}+\gamma^2u=0\tag{4}$$ with $$\gamma=\frac{\sqrt{2mE}}{\hbar}\tag{B}$$
But here is the problem: Equations $(4)$ and $(\mathrm{B})$ cannot be correct since I know that there must be an exponential fall-off outside the well.
I used the same mathematics to derive $(4)$ & $(\mathrm{B})$ as $(3)$ & $(\mathrm{A})$. After an online search I found that the correct equations are $$\fbox{$\frac{d^2 u}{dx^2}-\gamma^2u=0$}$$ and $$\fbox{$\gamma=\frac{\sqrt{-2mE}}{\hbar}$}$$
Looks like I am missing something very simple. If someone could point out my error or give me any hints on how I can reach the boxed equations shown above it would be greatly appreciated.
EDIT:
One answer mentions that the reason for the sign error is due to the fact that $E\lt 0$ inside the well, so I have included a graph showing the total energy (which is always less than zero inside or outside the well):
EDIT #2:
In response to the comment below. If I place $E\lt 0$ in equation $(4)$ (outside the well) I will have to also make $E\lt 0$ in equation $(3)$ (as $E\lt 0$ inside the well also) and so equation $(3)$ will become $$\frac{d^2 u}{dx^2}-k^2u=0$$ which is clearly a contradiction as this no longer gives oscillatory solutions (plane waves) inside the well.