title: PHP Form submit with arrays as rows
body: <p>I have the following form in php, but I want to make the <code>insert.php</code> action, to insert everything in a new row in MySQL... I don't know how to do it, because there are several fieldsets, it's easy with one simple form, but not for each <code>$_cliente</code> and <code>$_servicio</code> </p>
  
  <p><strong>CODE UPDATE</strong></p>
  
  <pre><code>&lt;?php
  require("database.class.php");
  //database server
  define('DB_SERVER', "localhost");
  
  //database usuario
  define('DB_USER', "root");
  //database password
  define('DB_PASS', "root");
  define('DB_DATABASE', "nivelservicio");
  $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
  $db-&gt;connect();
      $sql    = "SELECT * FROM servicios";
      $_row = $db-&gt;query($sql);
      $_servicios = array();
      while($row = $db-&gt;fetch_array($_row)) {
          $_servicios[$row['id']] = $row['nombre'];
      }
  
      $sql    = "SELECT * FROM clientes";
      $_row = $db-&gt;query($sql);
      $_clientes = array();
      while($row = $db-&gt;fetch_array($_row)) {
          $_clientes[$row['id']] = $row['nombre'];
      }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;&lt;/title&gt;
          &lt;style&gt;
              body{
                  margin:0;
                  border:0;
              }
              #formfieldset{
                  width: 350px;
                  font-family: helvetica, sans-serif, verdana;
                  font-size:12px;
              }
  
              h1 {
                  font-size:36px;
                  font-family: Georgia;
                  font-style: italic;
                  border-bottom:1px solid #000;
              }
  
              h2 {
                  font-size:18px;
                  font-color:#1E1E1E;
              }
  
  
          &lt;/style&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form action="insert.php" method="post"&gt;
          &lt;div id="formfieldset"&gt;
              &lt;?php foreach ($_clientes as $key_cliente =&gt; $cliente) :
                  switch ($cliente) {
                      case "Produban":
                          $_servicios = array_diff($_servicios, array(
                              "GESTI",
                              "VULCANO"
                          ));
                          break;
                  } ?&gt;
  
                  &lt;h1&gt;&lt;?php echo $cliente ?&gt;&lt;/h1&gt;
                  &lt;input type='hidden' name='cliente_id[&lt;?php echo $key_cliente ?&gt;' value='&lt;?php echo $key_cliente ?&gt;'&gt;
                  &lt;?php foreach($_servicios as $key_servicio =&gt; $servicio) : ?&gt;
                      &lt;h2&gt;&lt;?php echo $servicio ?&gt;&lt;/h2&gt;
                      &lt;input type="hidden" name="servicio_id[&lt;?php echo $key_cliente ?&gt;][&lt;?php echo $key_servicio ?&gt;]" value="&lt;?php echo $key_servicio; ?&gt;"&gt;
                      &lt;label&gt;Estado&lt;/label&gt;
                      &lt;select name="estado[&lt;?php echo $key_cliente ?&gt;][&lt;?php echo $key_servicio ?&gt;"&gt;
                          &lt;option value="OK"&gt;OK&lt;/option&gt;
                          &lt;option value="KO"&gt;KO&lt;/option&gt;
                      &lt;/select&gt;
                      &lt;label&gt;Texto&lt;/label&gt;&lt;input type="text" name="texto"&gt;
                  &lt;?php endforeach; ?&gt;
              &lt;?php endforeach; ?&gt;
  
              &lt;br&gt;
              &lt;input type="submit" name="insertar" value="Insertar"&gt;
          &lt;/div&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php 
  $db-&gt;close();
  ?&gt;
  </code></pre>
  
  <p><strong>insert.php</strong></p>
  
  <pre><code>if(isset($_POST['insertar'])){
      var_dump($_POST);
  
      foreach ($clientes as $key_cliente =&gt; $cliente) {
          foreach ($servicios as $key_servicio =&gt; $servicio) {
              //echo $_POST['estado'][$key_cliente][$key_servicio] . '&lt;br /&gt;';
              $sql = "INSERT INTO datos
                  (servicio_id, cliente_id, estado, texto, fecha)
                  VALUES
                  ('".$_POST['servicio_id'][$key_cliente][$key_servicio]."',
                      '".$_POST['cliente_id'][$key_cliente][$key_servicio]."',
                          '".$_POST['estado'][$key_cliente][$key_servicio]."',
                              '".$_POST['texto'][$key_cliente][$key_servicio]."',
                                  '".date('d-m-Y')."'
              )";
  
              $db-&gt;query($sql);
          }
      }
      echo "Valores insertados"; 
  }
  </code></pre>