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

I have total 10 rows like this

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

I'm trying to insert all the records in database in one queryw how can I insert all the rows in database by one query ?

Afzal Khan
  • 125
  • 2
  • 14

1 Answers1

0

If you want to do it using loop

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

    }


}

Otherwise you can use multiple insert query

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');
Mithu CN
  • 605
  • 5
  • 11