当date为null时,mysqli不会插入

时间:2013-10-22 05:01:00

标签: php mysqli

在我的表中,有一个名为receivedOn的列,它是一个日期类型。所以,如果我这样做了

$receivedOn= "2013-11-11";
$stmt = $dbi->prepare("insert into mytable(somedata,receivedOn) values (?,?)");
$stmt->bind_param('ss',$somedata,$receivedOn); 
$result = $stmt->execute() or die (mysqli_error($dbi)");

它会成功插入数据,但是如果$ receivedOn为空,它将不会插入数据,看起来就像挂在那里一样。没有返回任何错误。即使receiveOn为空,我怎样才能使mysqli插入数据?我可以使用普通的mysql查询(非mysqli)

使用emtpy receiveOn插入数据

1 个答案:

答案 0 :(得分:1)

尝试做:

$receivedOn = ($receivedOn != '') ? $receivedOn : NULL;
$stmt = $dbi->prepare("insert into mytable(somedata,receivedOn) values (?,?)");
$stmt->bind_param('ss',$somedata,$receivedOn); 
...