当我将字段留空时,如何写入NULL而不是0

时间:2015-08-02 20:37:39

标签: php

我在插入数据时遇到问题。当我将一个字段留空时,他在我的数据库中写入0而不是0.我要做的就是写入NULL。

<?php

if ($date == '' || $name_home == ''|| $name_away == '')
{

$error = 'Please enter the details!';

valid($date, $name_home, $name_away, $score_home, $score_away, $error);
}
else
{

mysql_query("INSERT name_matches SET date='$date', name_home='$name_home', name_away='$name_away', score_home='$score_home', score_away='$score_away'")
or die(mysql_error());

header("Location: view.php");
}
}
else
{
valid('','','','','','');
}
?>

1 个答案:

答案 0 :(得分:1)

它已经解决,可以关闭。

mysql_query("INSERT name_matches SET date='$date', name_home='$name_home', name_away='$name_away', score_home=IF('$score_home'='',NULL,'$score_home') ,score_away=IF('$score_away'='',NULL,'$score_away')") or die(mysql_error());