Well, let's take a few suggestions from Debugging 101 (in fact some points from the Guidelines wouldn't go amiss here, either).
The first two tips in the Debugging thread and philipolson's post in particular.
After all, how likely do you think it would have been for someone to go trawling through all that code to see the error in
$sql .= "`rarity";
$sql .= "$rarity";
$sql .= "'";
?
But there is a cleaner solution, and that is to use an array and a loop.
$field1 = (isset($_POST['field1']) ? $_POST['field1'] : '';
// Check that $field1 contains valid data,
// and not something designed to trash the query
$field2 = (isset($_POST['field2']) ? $_POST['field2'] : '';
// Check that $field2 contains valid data,
// and not something designed to trash the query
}
...later...
$fields = array('field1', 'field2', 'field3', ...);
foreach($fields as $field)
if ($$field != ""){
$sql .= "`$field` = '".$$field."'";
}