【问题标题】:How can I insert multiple rows in one query? [duplicate]如何在一个查询中插入多行? [复制]
【发布时间】:2017-07-13 10:31:12
【问题描述】:
<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."') ");

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

【问题讨论】:

  • insert into &lt;tablename&gt; (col1, col2...) values (val1, val2...), (val1, val2...).
  • 循环或者数组有什么办法吗?
  • 循环不是一个更好的主意。 SougataBose 提出了正确的方法

标签: php mysqli


【解决方案1】:

如果你想使用循环来做到这一点

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

【讨论】:

  • OP 需要 1 个查询。
猜你喜欢
  • 2010-10-01
  • 2017-01-15
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 2018-12-17
  • 2013-04-29
  • 2014-06-26
相关资源
最近更新 更多