数组的PHP数组后 [英] PHP Post array of array

查看:135
本文介绍了数组的PHP数组后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变从HTML POST请求两个不同的表到一个服务器上运行PHP。之前,当我只是想改变一个表,所有列和值放入POST数组,所以我会 [colname1] => VAL1 [colname2] = val2的等。然后,我会遍历POST建立正确的MySQL的语句来运行查询。

I would like to alter two different tables from an HTML post request to a server running PHP. Before, when I only wanted to alter one table, all the columns and values were put into the POST array, so I would have [colname1]=>val1, [colname2]=val2, etc. I would then loop through the POST to build the correct MySQL statement to run the query.

我修改我的code,使取决于什么下拉框中选择,不仅会第一个表被修改,但另一个表(用户选择的)将被修改为好。所以,我非常希望在PHP中POST变量有数组的数组。所以最终,这将是很好,如果POST变量看起来像这样...

I've modified my code so that depending on what drop down box is selected, not only will the first table get modified, but another table (of the users choice) will get modified as well. So ideally I would like the POST variable in PHP to have an array of arrays. So in the end, it would be nice if the POST variable looked like this...

[mainTable][colnam1]=>val1 [colnam2]=>val2
[otherTable][colnam1]=>val1 [colnam2]=>val2

我目前的解决办法是在其表单输入名称前面给表的名称,然后解析它在服务器上,但我宁愿更灵活,不会做这种方式。

My current workaround is having to give the table name in front of the the form input name and then parsing it on the server, but I would rather be more flexible and not do it this way.

<input type="text" name="mainTable_colname">
<input type="text" name="otherTable_colname">

从我的使用形式和POST的了解,只有指定的HTML表单元素将被传递成后变量。

From my understanding of using forms and POST, only the named html form elements will be passed up into the post variable.

有谁知道这样做的更好的办法?

Does anyone know a better way of doing this?

推荐答案

指定您元素的名称,属性是数组,包括你要的钥匙,你会得到的 $阵列_ POST

Specify the name attribute of your elements to be arrays, including the keys you want, and you'll get arrays in $_POST:

<input type="text" name="mainTable[colname]">
<input type="text" name="mainTable[colname2]">
<input type="text" name="otherTable[colname]">

A 的var_dump($ _ POST ['mainTable']); 会产生类似于:

array( 'colname' => 1, 'colname2' => 1);

这篇关于数组的PHP数组后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆