0

can't figure out how to insert multiple inputs to database using mysql. what am trying to do is to make an order form where you could insert multiple items each item has a quantity and price to it. should i use the same field name for each input followed with [] to make it an array. then how would the insert sql would look like?

<form action="processinvoice.php" method="post">

<label>Invoice Number</label>
    <input type="text" name="invoicenumber" value="Enter Invoice Number">
<p><label>Vendor's ID</label>
    <input type="text" name="vendorid" value="Enter Vendor's ID"></p>

    <!-- Product code begining -->
    <label>Items sold</label>
    <select name="proid[]" >

<?php

    $stmt = $dbcon->prepare("SELECT * FROM products");
    $stmt->execute(); 
    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        ?>
        <option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>

<?php
    } ?>
    </select> 
    <label>Quantity</label>
    <input type="text" name="tquantity[]" value="Enter quantity">
    <label>Price</label>
    <input type="text" name="saleprice[]" value="Enter price">
    <p></p><select name="proid[]" >

<?php

    $stmt = $dbcon->prepare("SELECT * FROM products");
    $stmt->execute(); 
    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        ?>
        <option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>

<?php
    } ?>
    </select> 
    <label>Quantity</label>
    <input type="text" name="tquantity[]" value="Enter quantity">
    <label>Price</label>
    <input type="text" name="saleprice[]" value="Enter price">
    <p></p><select name="proid[]" >

<?php

    $stmt = $dbcon->prepare("SELECT * FROM products");
    $stmt->execute(); 
    while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        ?>
        <option value=" <?php echo $pid; ?> "><?php echo $pname; ?></option>

<?php
    } ?>
    </select> 
    <label>Quantity</label>
    <input type="text" name="tquantity[]" value="Enter quantity">
    <label>Price</label>
    <input type="text" name="saleprice[]" value="Enter price">
    <!-- Product code END -->



    <input type="submit" name="submit" value="submit">

WHAT AM TRYING TO DO IS: inserting multiple items connected with quantity and price for each. all of the items will be showing the same invoice number and vendor id.

  • Possible duplicated by https://stackoverflow.com/questions/452859/inserting-multiple-rows-in-a-single-sql-query – Richard Jun 23 '19 at 12:01
  • what about the form part of the question? am i doing the form right? do i need a loop to check each field for entery? – Hashim Alsadah Jun 23 '19 at 13:01
  • Possible duplicate of [Inserting multiple rows in a single SQL query?](https://stackoverflow.com/questions/452859/inserting-multiple-rows-in-a-single-sql-query) – Oleksii Filonenko Jun 23 '19 at 14:31

1 Answers1

0

If you want to insert multiple records in your MySQL DB:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

C.B.
  • 208
  • 2
  • 7
  • any loops neccessery to do it? for an array? how about the form? – Hashim Alsadah Jun 23 '19 at 12:57
  • You need a loop to extract the form data from the php array. Within the same loop you may do one SQL INSERT for every record. Or you append a (...) to one big INSERT statement that you execute afterwards. – C.B. Jun 23 '19 at 13:27