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