-1

So I have this three lines, and the problem is that only the last one is updating. What am I doing wrong? What can I do to them to update all three at the same time?

$query = "UPDATE arak SET ara = '$konyha' WHERE ID = 1";
$query = "UPDATE arak SET ara = '$kugli' WHERE ID = 2";
$query = "UPDATE arak SET ara = '$ronk' WHERE ID = 3";
chris85
  • 23,846
  • 7
  • 34
  • 51
  • I believe this post already answers your question. http://stackoverflow.com/questions/3432/multiple-updates-in-mysql – Angrist Jan 21 '17 at 20:56
  • Possible duplicate of [Multiple Updates in MySQL](http://stackoverflow.com/questions/3432/multiple-updates-in-mysql) – Julian Jan 21 '17 at 21:06
  • you are just overriding the existing variable seems like . please add more example – nikoss Jan 22 '17 at 02:35

2 Answers2

3

You should execute each single query otherwise you execute only the last query assigned at $query

$query = "UPDATE arak SET ara = '$konyha' WHERE ID = 1"; 
your_execute_command() ... ;
$query = "UPDATE arak SET ara = '$kugli' WHERE ID = 2";
your_execute_command() ... ;
$query = "UPDATE arak SET ara = '$ronk' WHERE ID = 3";
your_execute_command() ... ;

or could yuo ca use a single query with a case clause

  UPDATE arak
  SET ara = case when ID = 1 then '$konyha' 
                 when ID = 2 then '$kugli'
                 when ID = 3 then '$ronk' 
            else ara
            end 
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
-1

Just combine these queries in a single string separated by terminator and use:

mysqli_multi_query($connection,$query);
user694733
  • 15,208
  • 2
  • 42
  • 68