I'm trying to write some php that will allow a user to edit an order they previously placed. But in the same way they made the order if that makes sense. So this is my jQuery code to send values to a php page:

$('#submit').live('click',function(){ 				
						var postData = {};
						postData['data[order_id]'] = $('#order_id').text();
						$('#items tr').not(':first').each(function(index, value) {
							var keyPrefix = 'data[' + index + ']';
							postData[keyPrefix + '[supp_short_code]'] = $(this).closest('tr').find('.supp_short_code').text();
							postData[keyPrefix + '[project_ref]'] = $(this).closest('tr').find('.project_ref').text();
							postData[keyPrefix + '[om_part_no]'] = $(this).closest('tr').find('.om_part_no').text();
							postData[keyPrefix + '[description]'] = $(this).closest('tr').find('.description').text();
							postData[keyPrefix + '[quantity_input]'] = $(this).closest('tr').find('.quantity_input').val();
							postData[keyPrefix + '[cost_of_items]'] = $(this).closest('tr').find('.cost_of_items').text();
							postData[keyPrefix + '[cost_total_td]'] = $(this).closest('tr').find('.cost_total_td').text();
						});

                    $.ajax
                        ({
                        type: "POST",
                        url: "updateorder.php",
						dataType: "json",
                        data: postData,
                        cache: false,
                        success: function()
                            {
								alert("Order Updated");
                            }
                        });
				});

This then sends values to this script:

if (isset($_POST['data']) && is_array($_POST['data'])) {
					foreach ($_POST['data'] as $row => $data) {
						$result = mysql_query("UPDATE orders SET project_ref='".$data['project_ref']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
						$result1 = mysql_query("UPDATE orders SET supp_short_code='".$data['supp_short_code']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
						$result2 = mysql_query("UPDATE orders SET om_part_no='".$data['om_part_no']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
						$result3 = mysql_query("UPDATE orders SET description='".$data['description']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
						$result4 = mysql_query("UPDATE orders SET quantity='".$data['quantity_input']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
						$result5 = mysql_query("UPDATE orders SET cost_of_items='".$data['cost_of_items']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
						$result6 = mysql_query("UPDATE orders SET cost_total='".$data['cost_total_td']."' where order_id = '".$data['order_id']."'") or die(mysql_error());
					}
				}

This is my mysql table structure:

id 	order_id 	project_ref 	supp_short_code 	om_part_no 	description 	quantity 	cost_of_items 	cost_total 	t_stamp
16 	1 	1 	1 	1 	1 	1 	1.00 	1.00 	2010-11-10 14:58:27
15 	1 	1 	1 	1 	1 	1 	1.00 	1.00 	2010-11-10 14:58:27

If that all makes sense above, at the moment when i submit to the php page, it sets every field to the order_id value(1). Can anyone think of a better way to do this??

First check the content of $_POST:

echo nl2br(var_export($_POST['data'],true));

at the start of your PHP script will do the trick - or if it doesn't display due to AJAX stuff, wrtie it to a file instead of echo.

I would have handled the PHP slightly differently, not to solve your current problem, but because I am lazy, and your method updates all the columns in the database for each element in the data array, causing a lot of unnecesary updates (49 instead of 1):

$query = "UPDATE orders SET ";
foreach($_POST['data'] as $var=>$val) {
  $query .= "$var = '$val',";
}
$query = substr($query,0,strlen($query)-1); // Remove the last comma
$query .= " where order_id = '{$data['order_id']}'";
$result = mysql_query($query) or die(mysql_error());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.