2012-10-20 9 views
-2

我最近開始使用PHP和MySQL,並且編寫了一個代碼來插入一個變量到MySQL列中,並且如果該列已經被填充(Not NULL)繼續並嘗試更新下一列。這裏是我的代碼:我的代碼有什麼問題? (如果前一列不爲空,則更新下一列)

$result=mysql_query(" 
UPDATE user_info SET 
Amount20 = (case when (Amount19 is not null and Amount20 is null) then ‘$amount’ WHERE Username ='$user' else Amount20 end) 

, Amount19 = (case when (Amount18 is not null and Amount19 is null) then ‘$amount’ WHERE Username ='$user' else Amount19 end) 

, Amount18 = (case when (Amount17 is not null and Amount 18 is null) then ‘$amount’ WHERE Username ='$user' else Amount18 end) 

, Amount17 = (case when (Amount16 is not null and Amount17 is null) then ‘$amount’ WHERE Username ='$user' else Amount17 end) 

, Amount16 = (case when (Amount15 is not null and Amount16 is null) then ‘$amount’ WHERE Username ='$user' else Amount16 end) 

, Amount15 = (case when (Amount14 is not null and Amount15 is null) then ‘$amount’ WHERE Username ='$user' else Amount15 end) 

, Amount14 = (case when (Amount13 is not null and Amount14 is null) then ‘$amount’ WHERE Username ='$user' else Amount14 end) 

, Amount13 = (case when (Amount12 is not null and Amount13 is null) then ‘$amount’ WHERE Username ='$user' else Amount13 end) 

, Amount12 = (case when (Amount11 is not null and Amount12 is null) then ‘$amount’ WHERE Username ='$user' else Amount12 end) 

, Amount11 = (case when (Amount10 is not null and Amount11 is null) then ‘$amount’ WHERE Username ='$user'else Amount11 end) 

, Amount10 = (case when (Amount9 is not null and Amount10 is null) then ‘$amount’ WHERE Username ='$user' else Amount10 end) 

, Amount9 = (case when (Amount8 is not null and Amount9 is null) then ‘$amount’ WHERE Username ='$user' else Amount9 end) 

, Amount8 = (case when (Amount7 is not null and Amount8 is null) then ‘$amount’ WHERE Username ='$user' else Amount8 end) 

, Amount7 = (case when (Amount6 is not null and Amount7 is null) then ‘$amount’ WHERE Username ='$user' else Amount7 end) 

, Amount6 = (case when (Amount5 is not null and Amount6 is null) then ‘$amount’ WHERE Username ='$user' else Amount6 end) 

, Amount5 = (case when (Amount4 is not null and Amount5 is null) then ‘$amount’ WHERE Username ='$user' else Amount5 end) 

, Amount4 = (case when (Amount3 is not null and Amount4 is null) then ‘$amount’ WHERE Username ='$user' else Amount4 end) 

, Amount3 = (case when (Amount2 is not null and Amount3 is null) then ‘$amount’ WHERE Username ='$user' else Amount3 end) 

, Amount2 = (case when (Amount1 is not null and Amount2 is null) then ‘$amount’ WHERE Username ='$user' else Amount2 end) 

, Amount1 = (case when (Amount1 is null) then '$amount' else Amount1 WHERE Username ='$user' end) 
"); 

我不知道什麼是錯,任何幫助將是偉大的! 注:我有一個理由有20個不同的列,請不要指向我「正常化」。

+0

是否有一個原因WHERE子句是機箱內部,而不是在最後結合整個查詢。 –

+0

我不確定在哪裏放。 – user1760791

+0

當你運行它時會發生什麼? – FoolishSeth

回答

0

我修改你的查詢,如下所示:

$result=mysql_query(" 
UPDATE user_info SET 
Amount20 = (case when (Amount19 is not null and Amount20 is null) then ‘$amount’ else Amount20 end) 
,Amount19 = (case when (Amount18 is not null and Amount19 is null) then ‘$amount’ else Amount19 end) 
,Amount18 = (case when (Amount17 is not null and Amount 18 is null) then ‘$amount’ else Amount18 end) 
,Amount17 = (case when (Amount16 is not null and Amount17 is null) then ‘$amount’ else Amount17 end) 
,Amount16 = (case when (Amount15 is not null and Amount16 is null) then ‘$amount’ else Amount16 end) 
,Amount15 = (case when (Amount14 is not null and Amount15 is null) then ‘$amount’ else Amount15 end) 
,Amount14 = (case when (Amount13 is not null and Amount14 is null) then ‘$amount’ else Amount14 end) 
,Amount13 = (case when (Amount12 is not null and Amount13 is null) then ‘$amount’ else Amount13 end) 
,Amount12 = (case when (Amount11 is not null and Amount12 is null) then ‘$amount’ else Amount12 end) 
,Amount11 = (case when (Amount10 is not null and Amount11 is null) then ‘$amount’ else Amount11 end) 
,Amount10 = (case when (Amount9 is not null and Amount10 is null) then ‘$amount’ else Amount10 end) 
,Amount9 = (case when (Amount8 is not null and Amount9 is null) then ‘$amount’ else Amount9 end) 
,Amount8 = (case when (Amount7 is not null and Amount8 is null) then ‘$amount’ else Amount8 end) 
,Amount7 = (case when (Amount6 is not null and Amount7 is null) then ‘$amount’ else Amount7 end) 
,Amount6 = (case when (Amount5 is not null and Amount6 is null) then ‘$amount’ else Amount6 end) 
,Amount5 = (case when (Amount4 is not null and Amount5 is null) then ‘$amount’ else Amount5 end) 
,Amount4 = (case when (Amount3 is not null and Amount4 is null) then ‘$amount’ else Amount4 end) 
,Amount3 = (case when (Amount2 is not null and Amount3 is null) then ‘$amount’ else Amount3 end) 
,Amount2 = (case when (Amount1 is not null and Amount2 is null) then ‘$amount’ else Amount2 end) 
,Amount1 = (case when (Amount1 is null) then '$amount' else Amount1 end) 
WHERE Username ='$user'");