Php/mysql update query

Hi,

Thanks in advance for a solution :slight_smile:

I’m sure I must have written something to do this in the past but I guess I’m having a mind blank.

I’m tying to write a query to update multiple columns in multiple rows in a MySQL table.


$update = $db->update("UPDATE ter SET 
                      ari = case id 
                            when '$sql->terr1' then (ter.ari-$move) 
                            when '$sql->terr2' then (ter.ari+$move)
                      end
                      WHERE id in ('$sql->terr1', '$sql->terr2')");
return "true";

The above works fine for updating a single column in multiple rows but I need to update multiple rows in multiple columns

I’ev played around like


$update = $db->update("UPDATE ter SET 
                          ari = case id when '$sql->terr1' then (ter.ari-$move) when '$sql->terr2' then (ter.ari+$move) end
                          name = case id when '$sql->terr1' then (ter.name-'test') when '$sql->terr2' then (ter.name+'test) end 
                          WHERE id in ('$sql->terr1', '$sql->terr2')");
      return "true";

Been looking at it for a while and done a lot of googling but totally stumped. I’m slowly coming to the conclusion it’s not possible but I like to think nothing is impossible when it comes ot MySQL :slight_smile:

Any help would be appreciated as I have about 25 queries to write… with 2 or 3 fields in each that’s 60+ queries :frowning:

Oli