PHP easy way to handle large number of form fields _php instances

Source: Internet
Author: User
On the form batch submission strategy in program development
A lot of times a form is too many fields, how to efficiently get form fields, but also for how to refresh the efficiency and unity of development?

For example a system has 26 fields, then I use the name of the form with 26 A to Z letters,

You are the choice,,......,Traditional form of doing it?

But if you do bulk data insertion in this case, it won't be that simple.
Because the insert or edit operation would be such a statement: Especially in this case, the egg-like SQL string is more painful.
Copy the Code code as follows:
$sql = "INSERT kele_table (A, b,......, z) value (a= ' $a ', b= ' $b ',......, z= ' $z ')";//This is long written Optimus is marked with ellipses
$sql = "UPDATE SET kele_table (a= ' $a ', b= ' $b ',......, z= ' $z ') where id= $id";

This kind of writing is very frustrating, the string is too long

It is better to use one of the following methods:
Point 1: Use the array pattern for the entire submitted form field.
Copy the Code code as follows:
,......,

Point 2:

PHP daemon receives $setting array via post

Point 3:

Insert Form Field Display

Copy the Code code as follows:
$fields =array (' A ', ' B ',......, ' z ');//This is deliberately set up a validation dictionary to verify the existence of the submitted field
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

Copy the Code code 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";

  • Related Article

    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.