Code to insert from a large form

I have a form with 25 fields that will have a value entered into each one and then written to MySQL. My insert statement is long and I don’t know what is a more effective way to right the insert statement without listing every field from the table and Post value. I would assume a loop.


foreach($_POST as $key => $value) {
$temp = is_array($value) ? $value : trim($value);

${$key} = $temp;
}



$resultQuery ="insert into canine_hemo (animal_id,test_type, hospital_id,vet_id,employee,test_date, white_blood_cell_count,seg_neutrophil_absolute,banded_neutrophil_absolute,lymphocyte_absolute,monocyte_absolute,eosinophil_absolute,basophil_absolute,other_absolute,blast_absolute,promyelocyte_absolute,myelocyte_absolute,metamyelocyte_absolute,red_blood_cell_count,hemoglobin,hematocrit,mcv,mch,mchc,platelet,segmented_neutrophils,banded_neutrophils,lymphocytes,monocytes,eosinophils,basophils)values('$animalName','canine_hemo','$hospital_ID','$vetID','$employeeName',NOW(),'$txt_White_Blood_Cell_Count','$txt_Seg_Neutrophil_Absolute','$txt_Banded_Neutrophil_Absolute','$txt_Lymphocyte_Absolute','$txt_Monocyte_Absolute','$txt_Eosinophil_Absolute','$txt_Basophil_Absolute','$txt_Other_Absolute','$txt_Blast_Absolute','$txt_Promyelocyte_Absolute','$txt_Myelocyte_Absolute','$txt_Metamyelocyte_Absolute','$txt_Red_Blood_Cell_Count','$txt_Hemoglobin','$txt_Hematocrit','$txt_MCV','$txt_MCH','$txt_MCHC','$txt_Platelet','$txt_Segmented_Neutrophils','$txt_Banded_Neutrophils','$txt_Lymphocytes','$txt_Monocytes','$txt_Eosinophils','$txt_Basophils')";




$result = $conn->query($resultQuery) or die(mysql_error());

}


The insecure, but quick way to do it:
Name all your form fields their db fieldnames.
“INSERT INTO canine_hemo(”.implode(‘,’,array_keys($_POST)).“) VALUES('”.implode(“‘,’”,array_values($_POST)).“');”

NOTE: Mysql implicitly converts between string and numbers. If your database does not, additional steps may be needed.

This is very insecure (Disclosing your database field names, and you should always sanitize input from forms, even if it’s an imperfect filter!) but it accomplishes what you were referring to.