简体   繁体   中英

INSERT multiple records in MySQL with one PHP

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.

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);

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