I used "variable variables" (note the double "$$" in the code) so that you would end up with 4 variables named "$email", "$gender", etc. However, there's really no particular reason to create a separate set of variables, since you already have the desired values in the $_POST array. Or you could create a separate array to store filtered/sanitized values. It depends to some degree on personal preferences.
$fields = array(
'email',
'gender',
'state',
'district'
);
foreach($fields as $fieldName) {
if(isset($_POST[$fieldName]) and trim($_POST[$fieldName]) !== '') {
$_POST[$fieldName] = trim($_POST[$fieldName]);
}
else {
$errors[] = "Please enter your $fieldName.";
}
}
// later...
$sql = sprintf(
"INSERT INTO some_table(email, gender, state, district) VALUES ('%s', '%s', '%s', '%s')",
mysql_real_escape_string($_POST['email']),
mysql_real_escape_string($_POST['gender']),
mysql_real_escape_string($_POST['state']),
mysql_real_escape_string($_POST['district'])
);