Convenient Method for PHP to process a large number of form fields _ php instance

Source: Internet
Author: User
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 ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.