使用数组使用jQuery,PHP和MySQL插入多行

I know this has been asked a million times, but I just can't seem to get it right. I am trying to insert multiple rows at the same time with data sent using jQuery.

I am using AJAX/jQuery to send values like this:

jQuery / AJAX

var extrasids = $('.classextrasid').map(function() {return $(this).val()}).toArray();
var extrasrates = $('.classextrasid').map(function() {return $(this).data('rates')}).toArray();
var extrasquantities = $('.classextrasid').map(function() {return $(this).data('quantity')}).toArray();

$.ajax({
        type: "POST",
        url: "modules/addsinglebooking.php",
        dataType: 'json',
        data: {extrasids:extrasids,extrasrates:extrasrates,extrasquantities:extrasquantities},
        cache: false,
        })

My PHP looks like this:

PHP

<?php
    session_start();
    $inputvalues = $_POST;
    $errors = false;
    $returnResult;
    $result = false;
    include_once '../../includes/database.php';

    $extraids = $inputvalues['extrasids'];
    $extrarates = $inputvalues['extrasrates'];
    $extraquantities = $inputvalues['extrasquantities'];


    if( !$errors ) {
        foreach($extraids as $extraid){
            $stmt = $mysqli->prepare("INSERT INTO `extras`(`extraid`,`extrarate`,`extraquantity`) values (?,?,?)");
            $stmt->bind_param('sss', $extraid,$extrarates,$extraquantities);
            if(!$stmt->execute()) echo $stmt->error;
        }
            $result = $stmt->get_result();
            $returnResult = "Success";
    }
            mysqli_close($mysqli);
            echo json_encode(['result' => $returnResult, 'errors' => $errors]);
            exit;
?>

My problem:

If my values looked like this, my above code would work:

extraid    extrarates    extraquantity
---------------------------------------
   1          rate1       quantity1
   2          rate1       quantity1
   3          rate1       quantity1

but all values are different like this:

extraid    extrarates    extraquantity
---------------------------------------
   1          rate1       quantity1
   2          rate2       quantity2
   3          rate3       quantity3

So I only to loop through one array and add multiple records if the rest of the values are the same and only one value is different.

How can I loop through everything to add 3 multiple rows which are unique?

In your foreach you can add this:

$i = 0;
foreach($extraids as $extraid){
        $stmt = $mysqli->prepare("INSERT INTO `extras`(`extraid`,`extrarate`,`extraquantity`) values (?,?,?)");
        $stmt->bind_param('sss', $extraid,$extrarates[$i++],$extraquantities[$i++]);
        if(!$stmt->execute()) echo $stmt->error;
}

So first setting $i to 0 and then in the loop increment it with each time it repeats