2012-10-10 85 views
1

可能重複:
illegal 0000-00-00 00:00:00 timestamp being sent by php to mysql using codeigniterSQL日期字段顯示爲0的

我想時間戳和日期/時間的組合查詢,但在查詢運行它加載0000-00-00 00:00:00進入日期字段。 查詢:

$link = mysqli_connect("$server", "$user", "$pass", "$webdb"); 
$title = mysqli_real_escape_string($link, (string) $_POST['title']); 
$content = mysqli_real_escape_string($link, (string) $_POST['content']); 
$query = "INSERT INTO `blog` (`title`, `content`, `posted_by`, `published`, `profile_pic`, `date`) 
      VALUES ('$title', '$content','$display_name', '1', '$profile_pic', 'getdate()')"; 

mysqli_query($link, $query); 
mysqli_close($link); 

有沒有人看到我在做什麼錯?提前致謝。

回答

1

'getdate()'被解釋爲一個字符串,不被MySQL理解。

使用NOW()但沒有引號。

$query = "INSERT INTO `blog` (`title`, `content`, `posted_by`, `published`, `profile_pic`, `date`) 
      VALUES ('$title', '$content','$display_name', '1', '$profile_pic', NOW())"; 
+0

謝謝;作爲臨時修復,我設置了一個變量$ d = date('M/d/Y')並在查詢中插入$ d;你會說遠離那個嗎? –

+0

@MorganGreen你正在使用'TIMESTAMP','date('M/d/Y')'只是返回日期,所以你還需要使用時間 –

+0

保持務實。這不是一個優雅的解決方案,但如果它有效,爲什麼不呢? – nalply

1

使用NOW()而不是使用您的SQL查詢getdate() ...

1

我不認爲getdate()正在接受評估。它試圖將字符串字面值"getdate()"插入表中,而不是管理,因此它會回落到0000-00-00 00:00:00

使用NOW()(沒有單引號)。