Why are these "undefined variables" after the sql?

droopsnoot gave you the answer to that, your conditional logic is foobar

Try changing the logical OR to logical AND .