A convenient way to process a large number of form fields in PHP, php form fields_PHP tutorial
WBOY
Release: 2016-07-13 10:07:44
Original
951 people have browsed it
PHP’s convenient way to handle a large number of form fields, php form fields
About form batch submission strategy in program development
Many times a form has too many fields. How can we obtain the form fields efficiently? How can we improve the efficiency and uniformity of development?
For example, if there are 26 fields in a system, then I use 26 letters from a to z in the name of the form,
You choose,,...,?
But in this case, batch data insertion will not be so simple,
Because the insertion or editing operation will be a statement like this: Especially such a painfully long SQL string is even more miserable.
Copy code The code is as follows:
$sql="INSERT kele_table(a,b,...,z) value(a='$a',b='$b',...,z='$z')";//It’s very long to write like this Tieniu is marked with an ellipsis
$sql="UPDATE SET kele_table(a='$a',b='$b',...,z='$z') where id=$id";
It’s quite troublesome to write like this, the string is too long
It is better to use one of the following methods:
Point 1: Use array pattern for the entire submitted form field.
Copy code The code is as follows:
,...,
Point 2:
PHP background program receives the $setting array via POST
Point 3:
Insert form field display
Copy code The code is as follows:
$fields=array('a','b',...,'z');//This is a verification dictionary specially set to verify whether the submitted fields exist
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 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";
http://www.bkjia.com/PHPjc/954656.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/954656.htmlTechArticlePHP is a convenient way to process a large number of form fields. PHP form fields are often related to the form batch submission strategy in program development. There are too many fields in the form, how to obtain the form efficiently...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn