i insist u look at the code
you were setting your $query variable once, .. before the loop. you needed to set it in each iteration of the loop.
you were also setting $task in your query string. that contained nothing.
in the loop, you then modified $task as it should have been, .. but the variable $task does not maintain its reference inside the $query string.