sql查询错误 - 逻辑或语法

时间:2012-11-05 05:41:43

标签: php 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'] . '" ');

上述查询是否有问题,或者我的输入中是否有空值?

感谢。

2 个答案:

答案 0 :(得分:1)

您可以阻止从脚本发送空字符串或尝试此

UPDATE community_table SET
    education = IFNULL($education, education) -- if null, maintain previous value

答案 1 :(得分:0)

只需在插入之前检查值 -

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