简体   繁体   中英

Dynamic multidimensional post form

I have a form with a variable number of inputs. The inputs are inside the table and I need to get three values from them: the row, the column and the actual value inside the input.

Some may be populated some may not and I need all their values to update a mysql db (row and column to know what to update, value to know the new value to insert in the database).

This is my form (an example version of it):

<form method="post" action="">
      <input name="data[111][222]" value="2" />
      <input name="data[112][221]" value="0" />
      <input name="data[113][223]" value="4" />
      //goes on
      <input name="data[324][435]" value="11" />
      <input name="data[325][436]" value="" />
</form>

And that's as far as I go. How can I get the data from this form so I can do a simple update in my database that goes like this (for all the affected inputs):

   update table set res="value_from_input" where row="row_value" and col="col_value"
<form method="post" action="">
      <input name="data[111][222]" value="2" />
      <input name="data[112][221]" value="0" />
      <input name="data[113][223]" value="4" />
      <input name="data[324][435]" value="11" />
      <input name="data[325][436]" value="" />
      <input type="submit" />
</form>
<?php
foreach ($_POST['data'] as $k1 => $v1)
{
    foreach ($v1 as $k2 => $v2)
    {
        echo "<p>k1:".$k1."; k2: ".$k2."; value: ".$v2."</p>";
        $query = "update table set res='$v2' where row='$k1' and col='$k2'";
        mysql_query($query);
    }
}
?>
foreach($_POST['data'] as $row => $row_array)
{
   foreach($row_array as $col => $value)
   {
      // Sanitize all input data before entry
      $mysql = "UPDATE table SET res='$value' WHERE row='$row' AND col='$col'";
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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