2013-10-22 42 views
0

在我的表中,有一個名爲receivedOn的列,它是一個日期類型。所以,如果我這樣做mysqli在日期爲空時不會插入

$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是空的,它不會插入數據,並會像它只是掛在那裏。沒有錯誤返回。我怎麼才能讓mysqli插入數據,即使當receivedOn爲空時?我可以使用正常的MySQL查詢(非mysqli的)

+5

你必須改變列接受空 – bksi

+0

是列已經可以接受空..這就是爲什麼我沒有問題,使用普通的MySQL查詢插入null receivedOn值..買不mysqli – imin

回答

1

與emtpy receivedOn插入數據嘗試這樣做:

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