Is the work done by external constant force = to potential energy stored in the spring.
But we know $F=kx$ but then $Fx = Kx^2$
Well see the definition of change in potential energy of the system.It is defined as the negative of work done by internal conservative forces of the system.
Here considering internal forces of the system we have only the spring force which varies with displacement from the reference position.
Now you have applied the equation for work done as F.X which is wrong to apply here as the force is variable in the system.
Now the infinitesimal work done by the spring force in extending the spring is ( -kx.dx ).When you will integrate this equation from 0 to x then you would get the required relation.
Hope this helps.