2016-07-28 180 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

不,我从来没有说你的答案是错误的;-)只是更具体一点。 –