Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当前时间戳_Php_Mysql - Fatal编程技术网

Php 当前时间戳

Php 当前时间戳,php,mysql,Php,Mysql,我已经阅读了许多关于当前时间戳的帖子,并尝试了许多提供的解决方案,但没有一个解决了我的问题。我需要将当前时间戳添加到数据库中。我认为我的代码可能是其他解决方案不起作用的原因。其他一切都完美无缺。时间戳只给了我所有的“0”,我尝试的其他解决方案给了我一个“第6行错误” 这是我目前的代码 <?php $con = mysql_connect("mysql","username","password"); if (!$con) { die('Could not connect:

我已经阅读了许多关于当前时间戳的帖子,并尝试了许多提供的解决方案,但没有一个解决了我的问题。我需要将当前时间戳添加到数据库中。我认为我的代码可能是其他解决方案不起作用的原因。其他一切都完美无缺。时间戳只给了我所有的“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)

?>

在PHP上下文中使用
当前时间戳
毫无意义

必须在表定义中包含所需的默认值。
大概是这样的:

CREATE TABLE listing
...
description VARCHAR(...)
creation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
...
然后,在PHP中,您应该只编写:

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

为了爱上帝,这是2016年。停止使用mysql API,开始使用参数化查询。阅读当前时间戳-另外,您还试图将其作为字符串输入。可能重复“在PHP上下文中使用当前时间戳没有意义!”-您的意思更像是:“在PHP上下文中使用
当前时间戳[子日期]”
“没有意义!”-
CURRENT\u TIMESTAMP()
NOW()
在用作MySQL核心函数时也可以使用,并且列的类型正确。@Fred ii-不确定您的意思。实际上,OP中使用的完整字符串毫无意义,但我没有尽力准确地报告它,因为:1)简单的
CURRENT_TIMESTAMP
在PHP中也没有意义;2) 它在SQL中是有意义的。令我困惑的是,你似乎认为1)以上是假的?不,我从来没有说过你的答案是假的;更具体一点。@Fred ii-无论如何,我觉得有必要精确地回答:看我的编辑。我看到了编辑,看到了你建议的新代码,不会输入当前时间,除非OP有AI'd列。正如我已经说过的,使用CURRENT_TIMESTAMP()或NOW()可以,但是OP的列必须是正确的类型。我们还没有接到行动组的消息,所以还悬而未决。我选择不发布这个问题的答案,因为有很多未知因素。祝你身体健康;-)