You really should be checking for errors in your query. Even something as basic as:
mysql_query($query) or die(mysql_error());
Any time you do a mysql function you should check for errors, or have a method of catching exceptions (if using an applicable interface, since the default mysql functions don't throw exceptions).
It could be messing up on a few points.
Also, as a hint, sprintf
works wonderfully if you're writing queries out.