这篇文章主要介绍了PHP处理大量表单字段的便捷方法,本文讲解一种使用数组的方式来快速、方便的处理大量表单数据,需要的朋友可以参考下
关于程序开发中的表单批量提交策略
很多时候一个表单太多的字段,如何能够高效获取表单字段,也为如何提神开发的效率和统一性?
比如一个系统的某个有26个字段,,那么我用表单的名称用26个a到z的字母,
你是选择 ,,……,的传统形式来做吗?
但是这种情况下如果做批量数据插入就不会有那么简洁了,
因为插入或者编辑操作会是这样的语句:特别是这样长得蛋疼的SQL字符串更悲催。
复制代码 代码如下:
$sql="INSERT kele_table(a,b,……,z) value(a='$a',b='$b',……,z='$z')";//这样写很长铁牛用省略号标示
$sql="UPDATE SET kele_table(a='$a',b='$b',……,z='$z') where id=$id";
这样子写的话挺折腾的,字符串太长
用下面一种方法更好:
要点1:对整个提交的表单字段使用数组模式。
复制代码 代码如下:
,……,
要点2:
PHP后台程序通过POST接收$setting数组
要点3:
插入表单字段展示
复制代码 代码如下:
$fields=array('a','b',……,'z');//这个是特意设置校验字典,校验提交的字段是否存在
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)";
更新表单字段展示
复制代码 代码如下:
$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";

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.