P. Jerome
P. Jerome

Reputation: 393

PHP/MySQL : UPDATE multiples values with multiples WHERE

The problem I'm having is with a strategy browser game which has 7 types of values. The problems are as follows:

  1. I got 7 different values in $_POST, from index unit_1 to index unit_7 included. These 7 values are INTEGERS between 0 and 20 and aren't static. They can at each call of the page. They represent the number of units sent.
  2. I got 7 lines in a table that does not follow the same schema : "nameUnit" = "Soldier" && nbUnit = "0", "nameUnit" = "Mage" && nbUnit = "5", etc..
  3. I need to do an update on the number of units in 1 request, but I don't know how to do this.

Example of what I want to do :

UPDATE x SET nbUnit = $_POST['u1'] WHERE nameUnit = "Soldier", nbUnit = $_POST['u2'] WHERE nameUnit = "Mage" [...]

Is it possible to do this that way and can it be done in a single query?

Upvotes: 0

Views: 57

Answers (2)

Hany Habib
Hany Habib

Reputation: 1405

IN PL SQL you can do the following :

UPDATE x SET nbUnit = case when nameUnit = "Soldier" then $_POST['u1'] 
when nameUnit = "Mage" then $_POST['u2'] else nbUnit end;

Upvotes: 0

Null Pointer
Null Pointer

Reputation: 466

this work in ur case

UPDATE x 
SET nbUnit = $_POST['u1'] 
WHERE (nameUnit = "Soldier" AND nbUnit = $_POST['u2']) OR 
(nameUnit = "Mage" AND nbUnit= $_POST['u5'])

Upvotes: 1

Related Questions