简体   繁体   English

处理多个表单输入字段

[英]process multiple form input fields

I have a form with 15 fields in it, and a table with same 15 fields names. 我有一个包含15字段的form ,以及一个具有相同15字段名称的table

In a form only 4 fields are mandatory and remaining 11 are not mandatory .. 在表单中,仅4字段是mandatory字段,其余11 4字段不是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.. 现在,我知道如何对数据库处理表单,因为我有4必填字段,因此我可以轻松对数据库进行处理。

but how i can process it when the form is coming to me with more then 4 fields to be processed againts database.. 但是当表单到来时,我将如何处理它,然后要处理4个以上的字段再次数据库。

i don't want to use if{} else{} here to avoid 11*11 combination with the 4 mandatory fields 我不想在这里使用if{} else{}来避免11*114必填字段的组合

my main purpose here is i don't want to use if else statement here.. 我在这里的主要目的是我不想在这里使用if else statement

any suggestion or help would be great help... thanks in advance 任何建议或帮助将是极大的帮助...在此先感谢

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. 对每个字段根据需要重复if语句,然后执行实际查询。

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. 替代方法是每次插入所有字段,并确保用户未填写的可选字段在插入之前设置为默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM