title: What is the best way to insert multiple form arrays into a MySQL database?
body: <p>I have written a PHP script that automatically generates and populates an HTML form.</p>
  
  <p>The HTML output is something like this:</p>
  
  <pre><code>&lt;form action="process.php" method="post"&gt;
      &lt;input type="hidden" name="product_name[]" value="product-1"&gt;
      &lt;input type="hidden" name="product_price[]" value="0.99"&gt;
  
      &lt;input type="hidden" name="product_name[]" value="product-2"&gt;
      &lt;input type="hidden" name="product_price[]" value="1.39"&gt;
  
      &lt;input type="submit" name="submit" value="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>My MySQL Database format is: <code>product_name, product_price</code></p>
  
  <p>What method should I use in <code>process.php</code> to insert this information into a MySQL database? </p>
  
  <p>If it were just the one array (for example, <code>product_name</code>) then I'd write the following code:</p>
  
  <pre><code>$product_name = $_POST['product_name'];
  
  foreach ($product_name as $n){
      $sql = "INSERT INTO orders (product_name)
      VALUES ('$n')";
      $conn-&gt;query($sql)
  }
  </code></pre>
  
  <p>But of course this foreach approach does not work with multiple arrays. I thought about and tried populating <code>product_name</code> first, and then creating foreach loop to UPDATE the <code>product_price</code> row, but this isn't working for me and feels very messy. I'm sure this isn't the best way to do this.</p>
  
  <p>What would be the correct way to handle this?</p>