From what I can tell the problem is probably in the two queries that look as follows:
mysql_query("DELETE FROM gallery WHERE id='$row[id]'");
When you access the $row array, you should use either $row['id'] or $row[$id], depending on if id is a variable or field name. So from what I see, your query should probably look like this:
mysql_query("DELETE FROM gallery WHERE id='".$row['id']."'");
One more thing you can do is to check out mysql_error(). It will report any errors associated with, well, MySQL. Hope this helps 😉