Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
you quote the whole '{$value}' . try it without like "INSERT ... values({$value})"); –  moskito-x Apr 29 at 13:51
    
Please describe the fields in your table sales_tickets and how they relate to the field in the array! –  RiggsFolly Apr 29 at 14:17
    
edited to show table –  Stuart Wickenden Apr 29 at 14:26
    
are you grabbing data from a html table and then post data in MySQL Database? –  Abdul Jabbar WebBestow Apr 29 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. –  Stuart Wickenden Apr 29 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.