I will assume that the field names in the form are also column names of the table,
a simplistic solution is this :
$input_names = '';
$input_values = '';
//Iterate the POST table to get the input names and values
foreach($_POST as $input_name => $input_value){
// escaping is always important
$input_names .= mysqli_real_escape_string ( $con, $input_name ) . ",";
$input_values .= "'" .mysqli_real_escape_string ( $con, $input_value ) . "',";
}
// Remove trailing comma
$input_names = rtrim( $input_names, "," );
$input_values = rtrim( $input_values, "," );
$sql = "INSERT INTO table_name ( $input_names ) VALUES ( $input_values )";
if ( $con->query($sql) === TRUE ) {
// Success
} else {
// Failure
}
In case there are input fields that are not part of the table, or actually in any case a check can happen in the field forming part. For example:
$field_array = ["field1", "field2", "field3"];
foreach($_POST as $input_name => $input_value){
// Skip field if the name is not in the $field_array
if(!in_array( $input_name, $field_array ){
continue;
}
$input_names .= mysqli_real_escape_string ( $con, $input_name ) . ",";
$input_values .= "'" .mysqli_real_escape_string ( $con, $input_value ) . "',";
}
The above code is untested and should only be used as a reference.