Build your query using string concatenation before executing it.
$qry = "INSERT INTO table_name SET required1 = 'value1', required2 = 'value2'; // etc
if (isset($optional_field)) $qry .= ", optional1 = 'optional_value1'";
Repeat the if statements as necessary for each field, then perform the actual query.
The alternative would be to insert all fields every time, making sure that optional fields the user did not fill out get set to their default values before inserting.