How to prove this statement about polynomials? Consider the following statement:

Let $F$ be an infinite field and $g$ be a given non-zero polynomial in $F[x_1,\ldots,x_r]$. If $f(x_1,\ldots,x_r)\in F[x_1,\ldots,x_r]$ and $f(a_1,\ldots,a_r) = 0$ for all $a_i$ such that $g(a_1,\ldots,a_r) \neq 0$, then $f$ is the zero polynomial.

I can see e.g. why this is true for $F = \mathbb R$, but the way I see it is extremely unhelpful - I see it through measure-theoretic means. I would like to prove this for any field and through purely algebraic means. Any hints on how to do this?
 A: Let me elaborate a bit on some of the ideas implicit in Jyrki Lahtonen's very slick answer.  In particular, I will give a concrete sense in which the vanishing set of a polynomial is "small", analogous to how the vanishing set of a polynomial over $\mathbb{R}$ has measure zero (in fact, this argument can be used to prove that measure-theoretic fact).
Specifically, we define by induction on $r$ what it means for a subset $V\subseteq F^r$ to be "small".  For $r=0$, we say $V\subseteq F^0$ is small if it is empty.  By induction, we then say that $V\subseteq F^r$ is small iff for all but finitely many $a\in F$, $\{b\in F^{r-1}:(b,a)\in V\}$ is a small subset of $F^{r-1}$.  So for instance, a subset of $F^1$ is small iff it is finite, and a subset of $F^2$ is small iff all but finitely many of its vertical fibers are finite.
Now I claim that if $f\in F[x_1,\dots,x_r]$ is any nonzero polynomial, then the set $V=\{v\in F^r:f(v)=0\}$ is a small subset of $F^r$.  We prove this by induction on $r$; the case $r=0$ is trivial.  Supposing $r>0$, think of $f$ as a polynomial in $x_1,\dots,x_{r-1}$ with coefficients in $F[x_r]$.  Since $f\neq 0$, at least one of these coefficients in $F[x_r]$ is nonzero.  There are thus only finitely many values which can be substituted for $x_r$ which make all the coefficients $0$.  Thus for all but finitely many $a\in F$, the polynomial $f_a(x_1,\dots,x_{r-1})=f(x_1,\dots,x_{r-1},a)$ is nonzero.  By induction, the set $\{b\in F^{r-1}:(b,a)\in V\}=\{b\in F^{r-1}:f_a(b)=0\}$ is small for all but finitely many $a$.  Thus $V$ is small.
Now finally note that the union of two small sets is small (easy by induction on $r$) and that if $F$ is infinite, then $F^r$ is not small as a subset of itself (again, easy by induction on $r$).  Thus if $f$ and $g$ are nonzero polynomials, the union of their vanishing sets is small and in particular is not all of $F^r$.  (Alternatively, as in the other answer, the union of their vanishing sets is the vanishing set of $fg$ and hence is small.)
This approach generalizes to give some stronger results than the simple, more algebraic approach in Jyrki Lahtonen's answer.  For instance, you could replace "finite" by "countable" to get another notion of "small" which is closed under countable unions.  This then shows that if $F$ is uncountable, no countable union of zero sets of polynomials can cover $F^r$.
A: Prove/combine the following


*

*If $h(x_1,x_2,\ldots,x_r)$ is not the zero polynomial then there exists $(a_1,a_2,\ldots,a_r)\in F^r$ such that $h(a_1,a_2,\ldots,a_r)\neq0$ (infinitude of $F$ is needed to prove this).

*The ring $F[x_1,x_2,\ldots,x_r]$ has no zero divisors.

*Apply item 1 to the product $h=fg$, and then...

