2012-11-05 40 views
2

目前我正在運行更新查詢正在工作,但它允許插入空值,我不想。SQL查詢錯誤 - 邏輯或語法

mysql_query('UPDATE community_table SET 
age = "'.$age.'", 
gender = "'.$gender.'", 
pregnant = "'.$pregnant.'", 
diet = "'.$diet.'", 
smoker = "'.$smoker.'", 
alcohol = "'.$alcohol.'", 
exercise = "'.$exercise.'", 
bmi = "'.$bmi.'", 
sleeping = "'.$sleeping.'", 
stress = "'.$stress.'", 
education = "'.$education.'" 
WHERE username = "' . $_SESSION['user'] . '" '); 

我試過這個查詢似乎可以防止輸入空值,但是它也可以防止在變量不爲空時輸入值。

age = "'.$age.'", 
gender = "'.$gender.'", 
pregnant = "'.$pregnant.'", 
diet = "'.$diet.'", 
smoker = "'.$smoker.'", 
alcohol = "'.$alcohol.'", 
exercise = "'.$exercise.'", 
bmi = "'.$bmi.'", 
sleeping = "'.$sleeping.'", 
stress = "'.$stress.'", 
education = "'.$education.'" 

WHERE age IS NOT NULL, 
gender  IS NOT NULL, 
pregnant IS NOT NULL, 
diet  IS NOT NULL, 
smoker  IS NOT NULL, 
alcohol IS NOT NULL, 
exercise IS NOT NULL, 
bmi  IS NOT NULL, 
sleeping IS NOT NULL, 
stress  IS NOT NULL, 
education IS NOT NULL and where 
username = "' . $_SESSION['user'] . '" '); 

上面的查詢是否有問題,或者我的輸入中是否有空值?

謝謝。

+0

? – Bajrang

+0

我不爲其分配新值的任何列。基本上任何以前的值都被轉換爲空值,除非我重新分配一個值給他們。他們不保留以前的價值。 – user1334130

回答

1

您可以防止空字符串從腳本發送或嘗試這種

UPDATE community_table SET 
    education = IFNULL($education, education) -- if null, maintain previous value 
在列查詢插入空值
+0

這聽起來很完美 - 我會給它一個去... – user1334130

+0

似乎沒有工作。在下一次測試之前,我在每行的末尾放置了一個「,」。那是對的嗎? – user1334130

+0

等待我想我修好了,這是其他地方的錯誤,謝謝 – user1334130

0

這樣插入之前只檢查值 -

UPDATE community_table SET 
    education = ifnull($education, education)