【发布时间】:2013-12-16 01:33:21
【问题描述】:
我有一个动态构建的表单:
foreach($xml->config->popup as $popup_item){
<input class="fm-req" id="fm-popup_name[]" name="fm-popup_name[]" type="text" value="" />
<textarea class="fm-req" rows="4" cols="50" id="fm-popup_desc[]" name="fm-popup_desc[]" /></textarea>
<input class="fm-req" id="fm-popup_image[]" name="fm-popup_image[]" type="file" />
}
我以前没有使用过表单名称中的数组,但我在另一篇关于堆栈溢出的帖子中看到你可以这样做,而且这似乎比我计划的要好得多,即将 $i 添加到名称的结尾并增加每个循环,所以我最终会得到:
fm-popup_name1
fm-popup_name2
fm-popup_name3 etc etc
然后我可以在构建表单时进行循环计数,将计数作为隐藏字段传递,然后使用 for 循环 where x
foreach($_POST['fm-popup_name'] as $index => $value) {
// get the value of name
}
foreach($_POST['fm-popup_desc'] as $index => $value) {
// get the value of name
}
foreach($_POST['fm-popup_image'] as $index => $value) {
// get the value of name
}
这样我可以访问我需要的所有数据,但我不想为 1 条记录进行 3 次单独的插入。
我如何获取上述信息以及类似的信息:
foreach($_POST['fm-popup_name,fm-popup_desc,fm-popup_image'] as $index => $value) {
INSERT INTO mytable(
popup_name,
popup_desc,
popup_image
)
VALUES(
'$popup_name',
'$popup_desc'
'$popup_image'
)";
}
有什么想法吗?希望代码没问题,我过滤掉了所有其他无关的废话,所以希望所有的 id 等都匹配,但我只是在寻找一个粗略的例子,我可以转换回我的代码。
【问题讨论】: