2012-11-05 155 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)