A handy way to handle a large number of form fields in PHP _php instance

Source: Internet
Author: User

On the form batch submission strategy in program development
Many times a form too many fields, how to efficiently get the form field, but also for how to refresh the development of efficiency and unity?

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

You are choosing <input type= "Text" Name= "a" >,<input type= "text" Name= "a" >,......, <input type= "text" name= "Z" > The traditional form to do it?

But in this case, if you do a lot of data insertion will not be so concise,
Because the insert or edit operation would be such a statement: In particular, such an egg-aching SQL string is more tragic.

Copy Code code as follows:

$sql = "INSERT kele_table (a,b,......, z) value (a= ' $a ', b= ' $b ',......, z= ' $z ')";//This is very long. Iron Ox with ellipsis mark
$sql = "UPDATE SET kele_table (a= ' $a ', b= ' $b ',......, z= ' $z ') where id= $id";

It's kind of frustrating to write, too long

Better in one of the following ways:
Point 1: Use array mode for the entire submitted form field.

Copy Code code as follows:

<input type= "text" Name= "Setting[a]" >,......, <input type= "text" Name= "setting[z]" >

Point 2:

PHP daemon receives $setting array via post

Point 3:

Insert Form Field Show

Copy Code code as follows:

$fields =array (' A ', ' B ',......, ' z ');//This is deliberately set up a checksum dictionary to verify that 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

Copy 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";

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.