I am creating an array, which contains data from a table.
var myarray = [];
$.each(multi, function (index, item) {
myarray.push( {name: 'ticket_row_num',
value: $(item).data('ticket_row_num')
} );
myarray.push( {name: 'itemtitle', value: $(item).data('itemtitle')} );
myarray.push( {name: 'row_itemid', value: $(item).data('row_itemid')} );
myarray.push( {name: 'row_quantity', value: $(item).data('row_quantity')} );
myarray.push( {name: 'rowunitprice', value: $(item).data('rowunitprice')} );
myarray.push( {name: 'row_total', value: $(item).data('row_total')} );
myarray.push( {name: 'measure_type_is_grams',
value: $(item).data('measure_type_is_grams')
} );
});
I then post this to PHP via AJAX.
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: {
myarray:myarray,
unique_reference:unique_reference,
tablenumber:tablenumber
},
beforeSend: function() {
// alert("before send");
},
success: function(html) {
alert(html);
}
});
I am having trouble inserting the data into MySQL correctly. I need to handle the array in the correct way to insert the correct values into the database at the correct time.
The PHP
$array = $_POST['myarray'];
if (is_array($array)){
foreach($array as $item){
// $name = $item['name'].$n;
$value = $item['value'].$n.$n;
mysql_query("INSERT INTO sales_tickets(value) values('{$value}')");
}
The issue is that this array is returning all of the values in a row. How can I manage this array better, so that each of the 7 values are inserted to a row respective of their rows?
The MySQL Table