当前位置: 首页 > 知识库问答 >
问题:

如何在一个查询中插入多行?[副本]

张高义
2023-03-14
<tr class="targetfields">
    <td><input type="text" class="form-control" name="order_id[]"  value="<?php echo $row3['id']; ?>"/></td>
    <td width="400"><textarea name="desc" class="form-control"><?php echo $item_description; ?></textarea></td>
    <td><input id="quant" class="form-control common quantity" name="quant[]" type="text" value="<?php echo $quantity; ?>"/></td>
    <td><input id="unit_price" class="form-control common price" name="unit_price[]"  type="text" value="<?php echo $unit_price; ?>"/></td>
    <td><input id="total" readonly class="form-control total" name="total[]" type="text" value=""/></td>
</tr>
<tr class="targetfields">
    <td><input type="text" class="form-control" name="order_id[]"  value="<?php echo $row3['id']; ?>"/></td>
    <td width="400"><textarea name="desc[]" class="form-control"><?php echo $item_description; ?></textarea></td>
    <td><input id="quant" class="form-control common quantity" name="quant[]" type="text" value="<?php echo $quantity; ?>"/></td>
    <td><input id="unit_price" class="form-control common price" name="unit_price[]"  type="text" value="<?php echo $unit_price; ?>"/></td>
    <td><input id="total" readonly class="form-control total" name="total[]" type="text" value=""/></td>
</tr>
<tr class="targetfields">
    <td><input type="text" class="form-control" name="order_id[]"  value="<?php echo $row3['id']; ?>"/></td>
    <td width="400"><textarea name="desc[]" class="form-control"><?php echo $item_description; ?></textarea></td>
    <td><input id="quant" class="form-control common quantity" name="quant[]" type="text" value="<?php echo $quantity; ?>"/></td>
    <td><input id="unit_price" class="form-control common price" name="unit_price[]"  type="text" value="<?php echo $unit_price; ?>"/></td>
    <td><input id="total" readonly class="form-control total" name="total[]" type="text" value=""/></td>
</tr>

<button  id='btn' name='btn' class="btnn primary-btn" type='submit'>Submit</button>

我总共有10行这样的

PHP

if(isset($_POST['btn'])){
    ob_start();
    $order_id = $_POST["order_id"];
    $desc = $_POST["desc"];
    $quant = $_POST["quant"];
    $unit_price = $_POST["unit_price"];
    $total = $_POST["total"];

    $query = mysqli_query($con,"insert into orders (id,description,quantity,unit_price,total) values('".$order_id."','".$desc."','".$quant."','".$unit_price."','".$total."') ");

我正在尝试在一个查询中插入数据库中的所有记录?我如何在一个查询中插入数据库中的所有行?

共有1个答案

袁旻
2023-03-14

如果要使用循环来执行

if(isset($_POST['btn'])){
    ob_start();
    foreach($_POST["order_id"] as $key =>$value){
        $order_id = $value;
        $desc = $_POST["desc"][$key];
        $quant = $_POST["quant"][$key];
        $unit_price = $_POST["unit_price"][$key];
        $total = $_POST["total"][$key];

        $query = mysqli_query($con,"insert into orders (id,description,quantity,unit_price,total) values('".$order_id."','".$desc."','".$quant."','".$unit_price."','".$total."') ");

    }


}

否则,您可以使用多个插入查询

INSERT INTO example
      (example_id, name, value, other_value)
    VALUES
      (100, 'Name 1', 'Value 1', 'Other 1'),
      (101, 'Name 2', 'Value 2', 'Other 2'),
      (102, 'Name 3', 'Value 3', 'Other 3'),
      (103, 'Name 4', 'Value 4', 'Other 4');
 类似资料:
  • 问题内容: 您好,我正在制作一个在pdo中进行多次插入的类。 是这样的 搜索后,我发现我必须建立类似 然后用这个执行 ,其中是 问题是我还得到一个错误就如何解决呢? 这是我正在做的一小段。 问题答案: 避免并发症的简单方法是这样的 但是,这将多次执行该语句。因此,最好创建一个较长的单个查询来执行此操作。 这是我们如何执行此操作的示例。

  • 问题内容: 假设我有两个表,并且 我想在一个查询中将来自某些输入的数据插入到表中,该怎么做? 请,如果可以做到,请解释语法。 问题答案: MySQL不支持在单个INSERT语句中进行多表插入。奇怪的是,Oracle是我所知道的唯一一个…

  • 问题内容: 我需要用一个查询插入多行(行数不是常数),所以我需要像这样执行查询: 我知道的唯一方法是 但我想要一些更简单的方法。 问题答案: 我构建了一个程序,该程序将多行插入到位于另一个城市的服务器上。 我发现使用此方法的速度大约是的10倍。就我而言,tup是一个包含约2000行的元组。使用此方法大约花了10秒钟: 使用此方法需要2分钟:

  • 问题内容: 我想编写脚本,具有对多个插入查询的功能。让我更好地解释一下。 我有一个html形式的输入。而且我有MySQL查询要插入到表中。所以我想让我的函数为“数量”次插入此查询。 例如要插入3次。有什么建议么? 问题答案: http://dev.mysql.com/doc/refman/5.5/zh- CN/insert.html

  • 我想获得如下数据 类别=“cat”和级别=“1” 这是我的密码 这是wrok,但如何在firbase中使用两个条件,我尝试两个,但它给出错误java.lang.IllegalArgumentException:您不能合并多个orderBy调用!

  • 我有以下疑问: 正如预期的那样,我得到了以下结果: 有没有办法将上述结果复制到另一个表中,使我的表看起来像这样? 我的问题是,可以预期任何数量的行,因此我不确定如何迭代未知数量的行。