【发布时间】:2016-08-26 03:17:46
【问题描述】:
我有一个 $_POST 数组,我正在尝试编写一个插入到 MySQL 数据库中的代码,而不必列出所有字段名称。
数组是单行,需要插入到数据库中。
数组如下所示:
Array
(
[membership_type] => 4
[title] => Mr
[first_name] => John
[last_name] => Smith
[known_as] => John
[address_1] => 10 High Street
[address_2] => Big House
[address_3] => Big Road
[address_4] => Chipping Sodbury
[address_5] => Bristol
[post_code] => BS37 1AB
[home_tel] => 01454 123456
[mobile] => 07777 123456
[email] => john@email.com
[confirm_email] => john@email.com
[day_dob] => 21
[month_dob] => 09
[year_dob] => 1974
[volunteer] => on
[employment_status] => employed
[college_nus] =>
[employment_address] => 50 Station Road
Chipping Sodbury
Bristol
BS37 2CD
[occupation] => Managerial/Professional
[employement_email] => john@work.com
[employement_phone] => 01454 654321
[terms] => 1
)
为方便起见,我已将表单字段名称编码为与数据库中的字段名称相对应。
非常感谢,
约翰
【问题讨论】:
-
这感觉是个坏主意。如果您只是遍历字段并插入用户提供的任何数据,您将如何处理每个字段级别的验证?
-
@MattRaines 该表单正在使用 Foundations 数据遵守,因此所有验证都在表单提交之前完成。
-
这篇文章可能对你有所帮助stackoverflow.com/questions/19665981/…
-
我不知道 Foundations 但它 looks like 一个 客户端 验证。如果您遵循此处的任何答案或链接的问题,您需要绝对确定用户没有提供名为
id) VALUES(1, 2, 3, 4, 5, ...); DROP TABLE Students; --的表单字段 -
@smnvhn - 我尝试了该帖子中的代码并得到一个致命错误:在答案代码的第 4 行调用未定义函数 insert_data() 错误:insert_data($mysqli, $array, $表名);