0

I am trying to pass an array of objects via ajax to my php file. Basically, once it gets to my php file I want to loop through every object in the array, and insert it's properties into a database.

Here is how I set up the array of objects that gets passed:

for(i=1;i<totalRowCount;i++){
    let currentRow = table.rows[i];
    itemArray.push({
        'itemId' : currentRow.cells[0].innerText,
        'memo' : currentRow.cells[1].innerText,
        'project' : currentRow.cells[2].innerText,
        'department' : currentRow.cells[3].innerText,
        'location' : currentRow.cells[4].innerText,
        'qty' : currentRow.cells[5].innerText,
        'price' : currentRow.cells[6].innerText
    })
}

Inside of my ajax call, I pass my data. All of the property values I pass in are just single values, except for itemArray which is my array of objects:

$.ajax({
    type: 'POST',
    url: 'upload.php',
    data:{
        'firstName' : firstName,
        'lastName' : lastName,
        'email' : email,
        'date' : date,
        'vendor' : vendor,
        'txnCurrency' : txnCurrency,
        'expDate' : expDate,
        'vendorDoc' : vendorDoc,
        'justification' : justification,
        'desc' : desc,
        'itemTotals' : itemTotals,
        'subTotals' : subTotals,
        'transTotal' : transTotal,
        'itemArray' : itemArray     
    },  
    success: function(data){
        alert('hi');
    }
});

My issue is I'm really not sure what format the itemArray will be when it gets to the php. I am trying to loop though right now, but I have somehow got it messed up:

    foreach($itemArray as $x) {
        $sql2 = $mysqli->prepare("INSERT INTO entries (po_id, memo, project_id, department, locn, qty, price) VALUES (?,?,?,?,?,?,?)");
        $sql2->bind_param($last_id,$x['memo'],$x['project'],$x['department'], $x['location'], $x['qty'], $x['price']);
        $conn->query($sql2);
    }

How would I format this so it works? Basically I want to make a separate insert into my database for every object inside of the array.

Eric Leslie
  • 51
  • 1
  • 1
  • 6
  • 1
    Just a pointer, try debugging in the network portion of your browsers dev tools. It will show the request and it's data. Off the top of my head, is it possible for you to json.stringify the data, pass it to PHP and in PHP json_decode? That's what I usually do :) – CodeJunkie Mar 31 '20 at 20:29
  • Why do you have `$conn->query($sql2);` instead of `$sql2->execute();`? – Dharman Mar 31 '20 at 22:03
  • What have you tried to make this work? It should not be too hard to dump `$_POST` and check whatever is in there – Nico Haase Apr 01 '20 at 12:24
  • Additionally, please don't use irrelevant tags. The given problem is not related to SQL or AJAX after all, if you haven't even checked what `$x` contains – Nico Haase Apr 01 '20 at 12:25

1 Answers1

0

I'm assuming the error ended up being in the formatting of my SQL statement. I also needed to JSON.Stringify my object before sending it to the php, and then using json_decode once it got there. Here was my solution syntax wise:

    foreach($fixedArray as $x) {
        $memo = $x['memo'];
        $project = $x['project'];
        $department = $x['department'];
        $location = $x['location'];
        $qty = $x['qty'];
        $price = $x['price'];

        $sql2 = ("INSERT INTO entries (po_id, memo, project_id, department, locn, qty, price) VALUES ('$last_id', '$memo', '$project', '$department', '$location', '$qty', '$price')");
        $conn->query($sql2);
        $counter = $counter + 1;
    }
Eric Leslie
  • 51
  • 1
  • 1
  • 6