0

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 The MySQL Table

4
  • you quote the whole '{$value}' . try it without like "INSERT ... values({$value})"); Commented Apr 29, 2014 at 13:51
  • Please describe the fields in your table sales_tickets and how they relate to the field in the array! Commented Apr 29, 2014 at 14:17
  • are you grabbing data from a html table and then post data in MySQL Database? Commented Apr 29, 2014 at 15:02
  • im grabbing strings stored in data- attributes within the tr of each row. Each row needs to be inserted to the mysql table respectively. Commented Apr 29, 2014 at 15:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.