2

目前我正在运行一个正在运行的更新查询,但是它允许插入我不想要的空值。

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'] . '" ');

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

谢谢。

4

2 回答 2

1

您可以防止从您的脚本发送空字符串或试试这个

UPDATE community_table SET
    education = IFNULL($education, education) -- if null, maintain previous value
于 2012-11-05T05:52:29.913 回答
0

只需在插入之前检查值 -

UPDATE community_table SET
    education = ifnull($education, education)
于 2012-11-05T05:52:07.547 回答