Fixed recursive mysql queries
bug fixes and input validation