简体   繁体   English

遍历多个POST元素

[英]Looping through multiple POST elements

I am trying to figure out how to loop through multiple POST form data that is dynamically pulled from a database and re-submit the modified data to a different table. 我试图弄清楚如何遍历从数据库动态提取的多个POST表单数据,然后将修改后的数据重新提交到另一个表。 For some reason (probably old age) I can't seem to come up with a solution that works. 由于某种原因(可能是高龄),我似乎无法提出有效的解决方案。

I am already looping out all the records from one table (call it roster) and need to submit it to another table (call it roster2). 我已经在一个表(称为名册)中循环出所有记录,并且需要将其提交到另一表(称为roster2)中。 The form is something similar to this: 形式类似于以下内容:

    <form name="name" action="form.php" method="post">
<input type="text" name="name1" value="15">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_12" value="12">

<input type="text" name="name2" value="8">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_6" value="6">
</form>

The 'name' and 'nameid' fields will always change and the number of records displayed will always be different (one day it could be 5 and the next 100). “名称”和“名称标识”字段将始终更改,并且显示的记录数将始终不同(一天可能是5条,下一天可能是100条)。 What is the best way to loop through the POST data to submit it to the database keeping all the associations intact? 遍历POST数据以将其提交给数据库并使所有关联保持完整的最佳方法是什么? I am relatively new to working with PHP and I can't seem to figure out a good way to do this. 我对使用PHP还是比较陌生,但似乎无法找出实现此目的的好方法。

You can loop through the fields and retain their keys like so: 您可以像这样遍历字段并保留其键:

foreach ($_POST as $field_name => $field_value) {
    // Storage
}

If you're going to generate them dynamically, I would recommend using PHP to place the ID of each form in the inputs on that form. 如果要动态生成它们,我建议使用PHP将每个表单的ID放在该表单的输入中。 So your original form would end up with these names: 因此,您的原始表单将以以下名称结尾:

<input type="text" name="name[12]" value="15">
<input type="text" name="attended[12]" value="1">
<input type="checkbox" name="nameid[12]" value="12">

<input type="text" name="name[6]" value="8">
<input type="text" name="attended[6]" value="1">
<input type="checkbox" name="nameid[6]" value="6">

Then your arrays will have keys corresponding to their form's ID. 然后,您的数组将具有与其表单ID相对应的键。 The array structure looks like this. 数组结构如下所示。

Array ( 
    [name] => Array ( [12] => 15, [6] => 8 ) 
    [attended] => Array ( [12] => 1, [6] => 1 ) 
    [nameid] => Array ( [12] => 12 [6] => 6 )
)

Now we need to figure out which ids are actually present today. 现在,我们需要找出今天实际存在的ID。 The array_keys() function generates an array of keys from any source array. array_keys()函数从任何源数组生成键数组。 Keys will be the same for each of the three elements, so I arbitrarily take the keys from [name]. 三个元素中每个元素的键都是相同的,所以我从[name]中任意取键。

$id_array = array_keys($_POST['name']);

Then, to access each element of the POST array, we'll use a foreach. 然后,要访问POST数组的每个元素,我们将使用一个foreach。

foreach ($id_array as $id) {

    //assign variables
    $name = $_POST['name'][$id];
    $attended = $_POST['attended'][$id];
    $nameid = $_POST['nameid'][$id];

   //store
   //Using whichever database style you like. I prefer PDO.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM