Why not create your whole INSERT statement via a sprintf before the 'do'? You could then check it for SQL syntax accuracy and also the contents of your variables (perhaps all you need is to use the $db->quote($sql_string) method).
What is in the $rows variable? BTW this $rows scalar is a little misnamed in that if no rows affected then the variable may well contain '0E0'- not 0! Which also of course is 'true' not 'false' - not necessarily what you want.