Question

I have a form with 15 fields in it, and a table with same 15 fields names.

In a form only 4 fields are mandatory and remaining 11 are not mandatory..

now how i can process the form against database as I know i have 4 fields mandatory so i can easily process it against database..

but how i can process it when the form is coming to me with more then 4 fields to be processed againts database..

i don't want to use if{} else{} here to avoid 11*11 combination with the 4 mandatory fields

my main purpose here is i don't want to use if else statement here..

any suggestion or help would be great help... thanks in advance

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top