2016-07-28 181 views
-1

我已經閱讀了許多關於當前時間戳的帖子,並嘗試了很多提供的解決方案,但是他們沒有解決我的問題。我需要將當前時間戳添加到數據庫中。我認爲我的代碼可能是其他解決方案無法正常工作的原因。其他一切都完美髮布。時間戳記給我所有的「0」,我嘗試的其他解決方案給了我一個「第6行錯誤」。當前時間戳

這裏是我當前的代碼

<?php 

$con = mysql_connect("mysql","username","password"); 

if (!$con) 

    { 

    die('Could not connect: ' . mysql_error()); 

    } 

    /* Prevent duplicate submissions */ 
if (isset($_COOKIE['FormSubmitted'])) 
{ 
show_error('You may only submit this form once per session!'); 
} 

mysql_select_db("seller", $con); 

$sql="INSERT INTO listing (name, email, website, url, dropdown, price, date, visitors, income, host, description) 

VALUES 

('$_POST[name]', '$_POST[email]', '$_POST[website]', '$_POST[url]', '$_POST [dropdown]', '$_POST[price]', '$_POST[date]','$_POST[visitors]', '$_POST[income]', '$_POST[host]', '$_POST[description]', 'CURRENT_TIMESTAMP[subdate]')"; 

if (!mysql_query($sql,$con)) 

    { 

    die('Error: ' . mysql_error()); 

    } 

echo "Thank you for listing with us. <a href="#">Explore Now</a>"; 


mysql_close($con) 

?> 
+3

對於'$ DEITY'的愛,這是2016年**停止使用'mysql_' APIS,並開始使用參數化查詢**。 –

+0

閱讀CURRENT_TIMESTAMP https://dev.mysql.com/doc/refman/5.5/zh-cn/date-and-time-functions.html#function_current-timestamp - 另外,您還嘗試以字符串形式輸入。 –

+1

可能重複[MySQL - insert current date/time?](http://stackoverflow.com/questions/19246309/mysql-insert-current-date-time) –

回答

0

在PHP方面使用CURRENT_TIMESTAMP已經沒有任何意義!

您必須在表格定義中包含所需的默認值。
事情是這樣的:

CREATE TABLE listing 
... 
description VARCHAR(...) 
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
... 

然後在PHP中,你應該只寫:

$sql=" 
    INSERT INTO listing (name, ..., description) 
    VALUES ('$_POST[name]', ..., '$_POST[description]') 
"; 
+0

*「在PHP上下文中使用CURRENT_TIMESTAMP毫無意義! 「* - 你的意思更像是:*」在PHP上下文中使用'CURRENT_TIMESTAMP [subdate]''沒有意義!「* - 當用作核心MySQL時,CURRENT_TIMESTAMP()或NOW()函數,並且列的類型是正確的。 –

+0

@ Fred-ii-不確定明白你的意思。事實上,沒有任何意義的是OP中使用的完整字符串,但我沒有努力報告​​它,因爲:1)_簡單'CURRENT_TIMESTAMP'在PHP中沒有意義; 2)它像SQL一樣有SQL意義。令我感到困惑的是,你似乎認爲上述1)是假的? – cFreed

+0

不,我從來沒有說你的答案是錯誤的;-)只是更具體一點。 –