When posting PHP code, please use the board's [noparse]
..
[/noparse] bbcode tags as they make your code much easier to read and analyze.
As for the problem, see the second 'useful link' in my signature.
EDIT: Forgot to mention... note that MySQL identifiers (names of tables, columns, databases, etc.) are to be delimited by either nothing at all (most common, unless you've accidentally(?) used one of the reserved words as an identifier, of course) or by using "`" backticks (or double quotes, if you've got the ANSI_QUOTES SQL mode enabled).
Single quotes, on the other hand, are used for delimiting strings.