This article mainly introduces PHP's convenient way to process a large number of form fields. This article describes how to use arrays to quickly and conveniently process large amounts of form data, for more information about the batch submission policy of forms in program development, see
Most of the time, how can I efficiently retrieve too many fields in a form and improve the efficiency and uniformity of development?
For example, if a system has 26 fields, I use the form name to use 26 letters a to z,
You choose,,......,Is it done in the traditional form?
However, in this case, batch data insertion will not be so concise,
This is because the insert or edit operation is like this: especially the SQL string that is so painful is even worse.
The code is as follows:
$ SQL = "INSERT kele_table (a, B ,......, Z) value (a = '$ A', B =' $ B ',......, Z = '$ Z') "; // enter the ellipsis for a long iron ox.
$ SQL = "update set kele_table (a = '$ A', B =' $ B ',......, Z = '$ Z') where id = $ id ";
The string is too long.
It is better to use the following method:
Key aspect 1: Use the array mode for the entire submitted form field.
The code is as follows:
,......,
Key aspect 2:
PHP background program receives $ setting array through POST
Key aspect 3:
Insert form field display
The code is as follows:
$ Fields = array ('A', 'B ',......, 'Z'); // This is a specially set validation dictionary to verify whether the submitted field exists.
Foreach ($ setting as $ k => $ v ){
If (in_array ($ k, $ fields) {$ sqlk. = ','. $ k; $ sqlv. = ", '$ V '";}
}
$ Sqlk = substr ($ sqlk, 1 );
$ Sqlv = substr ($ sqlv, 1 );
$ SQL = "INSERT INTO kele_table ($ sqlk) VALUES ($ sqlv )";
Update form field display
The code is as follows:
$ SQL = '';
Foreach ($ setting as $ k => $ v ){
If (in_array ($ k, $ fields) $ SQL. = ", $ k = '$ V '";
}
$ SQL = substr ($ SQL, 1 );
$ SQL = "UPDATE kele_table SET $ SQL WHERE id = $ id ";