title: How can I INSERT multiple rows into a table using PHP and MYSQL?
body: <p>I need to <code>INSERT INTO</code> two different tables with two different <code>INSERT</code> statements. The first one works fine and the second one works as well, however, there can several rows to <code>INSERT INTO</code> the second table but this only inserts one row. I thought a <code>for</code> loop would work but it doesn't give me what I need.</p>
  
  <pre><code>$productID = $_POST['product_id'];
  $title = $_POST['title'];
  $productQty = $_POST['qty'];
  
  if(empty($hNum) || empty($street) || empty($city) || empty($county) || empty($postcode) || empty($country)){
    header("Location: checkout.php?error=emptyaddressfields");
    exit();
  }
  else {
    $sql = "INSERT INTO orders (customer_id, order_date, order_status, num_items, total_cost)
                VALUES ('$customer_id', CURRENT_TIMESTAMP, 'Order Placed', '$num_items', '$total_cost')";
  
    if(mysqli_query($conn, $sql)){
      for($i=0; $i &lt; $productQty; $i++) {
        $sqlOI = "INSERT INTO order_item (order_id, product_id, title, quantity)
                  VALUES (
                    (SELECT MAX(order_id) FROM orders),
                    '$productID',
                    '$title',
                    '$productQty'
                    )";
      }
      if(mysqli_query($conn, $sqlOI)){
        header("Location: account.php?success=orderPlaced");
        exit();
      } 
      else {
        echo "Something went wrong...".mysqli_error($conn);
      }
    }
  }
  </code></pre>
  
  <p>If there is a different kind of loop that would work better, please let me know. I have searched online for different ways to do this but either they don't make sense or don't work. If I run the second <code>INSERT</code> statement in command line (with out the variables) it works so I know the problem isn't the <code>SQL</code>.</p>
  
  <p>Any help is much appreciated.</p>
  
  <p><strong>EDIT</strong>:
  Data is displayed inside a table, and a new row is added when another product has been added to the session array.
  Each row in the HTML table is generated by a product being added to the "cart". Each row has the same <code>&lt;tr&gt;</code> <code>&lt;td&gt;</code> and <code>&lt;input&gt;</code> attributes. So when another product is added, it generates a new row, with the same <code>name="id"</code> or whatever the name is. </p>
  
  <pre><code>&lt;?php
     if(!empty($_SESSION['shopping_cart'])):
       $total = 0;
       $cart_qty = 0;
       foreach($_SESSION['shopping_cart'] as $key =&gt; $product):
  
       &lt;tr&gt;
        &lt;td hidden&gt;&lt;input type="text" name="product_id" value="&lt;?php echo $product['product_id']; ?&gt;"&gt;&lt;/td&gt;
         &lt;td&gt;&lt;input type="text" name="title" value="&lt;?php echo $product['title']; ?&gt;"&gt;&lt;/td&gt;
         &lt;td&gt;&lt;input type="text" name="qty" value="&lt;?php echo $product['quantity']; ?&gt;"&gt;&lt;/td&gt;
         &lt;td&gt;£&lt;?php echo $product['price']; ?&gt;&lt;/td&gt;
         &lt;td&gt;£&lt;?php echo number_format($product['quantity'] * $product['price'], 2); ?&gt;&lt;/td&gt;
       &lt;/tr&gt;
  
       $total = $total + ($product['quantity'] * $product['price']);
       $cart_qty = $cart_qty + ($product['quantity']);
     endforeach;
  ?&gt;
  </code></pre>