插入时间到数据库(Inserting time into database)

2019-09-29 06:14发布

下面的代码是不工作是应该的方式

$t = date('Y-d-m H:i:s',time());
$query = "INSERT INTO time VALUES('$t')";
if(mysql_query($query))
echo "Date and Time are added";

我创建了一个名为表time只有一列nowDATETIME数据类型 。 即使PHP脚本通过打印成功执行,在浏览器中"Date and Time are added"

该数据库没有更新它应该是的方式。 而不是当前的时间 ,它就会被初始化为默认值。

不要建议纠正问题的方式。

Answer 1:

现在,您可以使用()

$query = "INSERT INTO `time` (`now`) VALUES (NOW())";


Answer 2:

<?php

/* YOU NEED TO ESTABLISH FIRST A CONNECTION */

$con=mysqli_connect("Host","Username","Password","Database"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

$t = date('Y-d-m H:i:s');
$query = mysqli_query($con,"INSERT INTO time (now) VALUES ('$t')");
echo "Date and Time are added";

?>


Answer 3:

这种格式尝试

$t = date('Y-m-d H:i:s');
$query = "INSERT INTO time (now) VALUES('$t')";
if(mysql_query($query))
echo "Date and Time are added";

要么

$query = "INSERT INTO time (now) VALUES(NOW())";


Answer 4:

改变的$ T从价值

$t = date('Y-d-m H:i:s',time());

$t = date('Y-m-d H:i:s');

更妙的方法是编写插入这样的查询:

   $query = "INSERT INTO `time` VALUES ('".date('Y-m-d H:i:s')."')";


文章来源: Inserting time into database