Wht's wrong with my code?


foreach($_POST['100type'] AS $key=>$value) { // UPDATE 100 TYPE
	mysql_query("UPDATE order_details_table SET block_quantity='{$_POST['100quantity'][$key]}', block_pallets='{$_POST['100pallets'][$key]}', block_extras='{$_POST['100extras'][$key]}' WHERE order_id='$id'")
    or die(mysql_error());
}

the above code updates table data. It does not give me any errors but it does not update the table data. It fills in zero instead of the number it should be.

this code for checking post data

echo '<pre style="text-align: left;">' . print_r($_POST, true) . '</pre>';

shows that the data is there, but it does not update the table. Here is the posted data:


...
[100type] => Array
        (
            [1] => 10.01
            [2] => 10.02
            [3] => 10.03
            [4] => 10.04
            [5] => 10.31
            [6] => 10.83
            [7] => 10.702
            [8] => 10.772
            [9] => 10.71
            [10] => 10.72
            [11] => 10.73
            [12] => 10.74
        )

    [100perpallet] => Array
        (
            [1] => 180
            [2] => 240
            [3] => 360
            [4] => 576
            [5] => 144
            [6] => 288
            [7] => 280
            [8] => 560
            [9] => 360
            [10] => 480
            [11] => 720
            [12] => 1152
        )

    [100quantity] => Array
        (
            [1] => 654
            [2] => 458
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )
...

One has to be able to inspect the evidence. Change

mysql_query( "UPDATE order_details_table SET block_quantity='{$_POST['100quantity'][$key]}', block_pallets='{$_POST['100pallets'][$key]}', block_extras='{$_POST['100extras'][$key]}' WHERE order_id='$id'") or die(mysql_error());

to

$sql = "UPDATE order_details_table
           SET block_quantity = '{$_POST['100quantity'][$key]}',
               block_pallets = '{$_POST['100pallets'][$key]}',
               block_extras = '{$_POST['100extras'][$key]}'
         WHERE order_id = '$id'";
echo $sql;
mysql_query($sql) or die(mysql_error());

While this changes nothing, it lets you see the actual query sent to MySQL

hi,
thanks for the reply.
this is the sql statement that is echoed: UPDATE order_details_table SET block_quantity = ‘0’, block_pallets = ‘0’, block_extras = ‘0’ WHERE order_id = ‘40’

But the post data has :


 [100quantity] => Array
        (
            [1] => 654
            [2] => 458
            [3] => 0
            [4] => 0
            [5] => 0
            [6] => 0
            [7] => 0
            [8] => 0
            [9] => 0
            [10] => 0
            [11] => 0
            [12] => 0
        )

So block_quantity should be =‘654’

here’s the code again


foreach($_POST['100type'] AS $key=>$value) {
$sql = "UPDATE order_details_table
           SET block_quantity = '{$_POST['100quantity'][$key]}',
               block_pallets = '{$_POST['100pallets'][$key]}',
               block_extras = '{$_POST['100extras'][$key]}'
         WHERE order_id = '$id'";}
echo $sql;
mysql_query($sql) or die(mysql_error());

No I am wrong.

block_quantity is ‘0’ cause its the last value in the array. the problem is that the actual table is not updated properly. All records get the last value of the post

Why would one order have 12 sets of values? I’m not sure what you are doing but the foreach loop had a different purpose. Shouldn’t each order have a single set of values?

Now it works:
here’s the code


foreach($_POST['100type'] AS $key=>$value) {
$sql = "UPDATE order_details_table
           SET block_quantity = '{$_POST['100quantity'][$key]}',
               block_pallets = '{$_POST['100pallets'][$key]}',
               block_extras = '{$_POST['100extras'][$key]}'
         WHERE order_id = '$id' AND block_type='{$_POST['100type'][$key]}'";
echo $sql;
mysql_query($sql) or die(mysql_error());
}

thanks for looking into this

Just to keep things neat and readable, and comment out the echo used for debugging…


foreach($_POST['100type'] AS $key=>$value) {
$sql = "UPDATE order_details_table
           SET block_quantity = '{$_POST['100quantity'][$key]}',
               block_pallets = '{$_POST['100pallets'][$key]}',
               block_extras = '{$_POST['100extras'][$key]}'
         WHERE order_id = '$id'
           AND block_type='{$_POST['100type'][$key]}'";
// echo $sql;
mysql_query($sql) or die(mysql_error()); 
}

thanks for looking into this

My pleasure!