Have you tried echo'ing out the dynamically produced query and executing it on the SQL server (through some method other than utilizing your PHP server)

Sometimes actually reading through the query once it is completely generated helps ascertain what is incorrect about it. From your description (and no knowledge about the content or structure of your database) your query looks correct and the type of slashes should not be causing the problem.

All of this is assuming that you are, as you say, not a complete idiot.