Question: I'm working on outputting data from two different sources into an HTML table. One source is a `mysql_fetch_array`, the other is a `$_POST` array. There are three `<td>` tables from mysql and one from `$_POST` outputting via a while loop. To associate the arrays on the output page, I planned to store the `$_POST` data in the table via the row id and then query the whole row further down the page.

The issue I have is that I can't seem to get the `$_POST` array values UPDATED into the row correctly. The last value in the `$_POST` array is all what's stored in all selected fields.

The input page arrays are captured with `id [ ]` and `unit_ass[ ]` and are passed to the output page.

The output page code:

```
require_once ("../includes/db_connect.inc");

    $pid = array_filter($_POST['id']);
    $gid = implode(",", $pid);

    $sua = array_filter($_POST['unit_ass']);

    foreach($sua as $value) echo $value . "<br>"; // this outputs correct values in the correct order.

    foreach($sua as $value) mysql_query("UPDATE $tbl_name SET unit_ass='$value' WHERE id IN ( $gid )"); 

```

Just stripping empty keys with `array_filter()` ... When I echo the values in the first foreach statement, they are displayed correctly and in correct order.

However, only the last value from the `$sua` array is updating the table rows as determined by $gid. The foreach isn't iterating the array values and performing the UPDATE as it should. The data via `id[ ]` is displayed correctly further down the page via a `mysql_fetch-array` in a while loop.

I've been staring at this for several hours now and can't seem to find the solution. Any help is appreciated.

Answer: 
```
foreach($sua as $key => $value){
    $value = mysql_escape_string($value);
    $id = mysql_escape_string ($pid[$key]);
    mysql_query("UPDATE $tbl_name SET unit_ass='$value' WHERE id='$id'");
}

```
Well, I believe `$gid = implode(",", $pid);` will give one value (consider as `1`)

Now suppose `$sua = array_filter($_POST['unit_ass']);` give you `ABC, PQR, LMN`.

so your query will be executed like this

```
 mysql_query("UPDATE $tbl_name SET unit_ass='ABC' WHERE id='1'");
 mysql_query("UPDATE $tbl_name SET unit_ass='PQR' WHERE id='1'");
 mysql_query("UPDATE $tbl_name SET unit_ass='LMN' WHERE id='1'");

```

as your `$gid` is same through-out, you are seeing Only last update... Hope you got your mistake... 

I believe you also want to change `gid`. For that check @Bao Nhan answer, that might help you as well!!!

**Update :**

Use below code... It will work now...

```
require_once ("../includes/db_connect.inc");

$sua = array_filter($_POST['unit_ass']);

 foreach($sua as $myKey => $value) {
     $myID = $pid[$myKey];
     mysql_query("UPDATE $tbl_name SET unit_ass='$value' WHERE id='$myID'");
 }

```

Good Luck!!!