【问题标题】:INSERT multiple records in MySQL with one PHP用一个 PHP 在 MySQL 中插入多条记录
【发布时间】:2019-06-23 11:57:57
【问题描述】:

无法弄清楚如何使用 mysql 将多个输入插入数据库。 我想做的是制作一个订单表格,您可以在其中插入多个项目,每个项目都有数量和价格。我应该为每个输入使用相同的字段名称,后跟 [] 使其成为一个数组。那么插入的 sql 会是什么样子呢?

<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">

我想做的是: 插入与每个项目的数量和价格相关的多个项目。 所有商品都将显示相同的发票编号和供应商 ID。

【问题讨论】:

标签: php html mysql insert


【解决方案1】:

如果您想在 MySQL 数据库中插入多条记录:

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

【讨论】:

  • 任何循环必须这样做?一个数组?表格呢?
  • 您需要一个循环来从 php 数组中提取表单数据。在同一个循环中,您可以为每条记录执行一次 SQL INSERT。或者您将 (...) 附加到您之后执行的一个大 INSERT 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多