Update Multiple Rows in one PHP

后端 未结 2 1533
独厮守ぢ
独厮守ぢ 2021-01-17 05:01

I have three rows with three columns in one table

id    type       value
1     gold       1000.00
2     silver     32.21
3     platinum   1500.00


        
相关标签:
2条回答
  • 2021-01-17 05:29

    You could use a CASE clause like this:

    $query = "UPDATE metals SET value = CASE id WHEN 1 THEN $gold  WHEN 2 THEN $silver WHEN 3 THEN $plat END";
    

    This seems a bit of overkill for just three rows, though.

    0 讨论(0)
  • 2021-01-17 05:33

    Why not use the metal name as your key?

    $update="UPDATE metals SET value = '".$gold."' WHERE type = 'gold'";
    

    And you really need to switch to prepared statements. You're wide open to SQL injection

    0 讨论(0)
提交回复
热议问题