599abfac00
Multiple issues fixed: * Fixed order of the statements to avoid access to undefined variables. * Fixed population of the SQL query parameter to make sure they match the placeholder in the query. * Fixed missing table aliases in the second query to make sure the query actually works as expected.