下面的代码不按预期的方式工作
$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
的表,其中只有一列now
和DATETIME
为数据类型。即使 PHP 脚本通过打印"Date and Time are added"
在浏览器中成功执行。
数据库没有按照预期的方式更新。它不是当前时间,而是初始化为默认值。
建议纠正问题的方法。
答案 0 :(得分:3)
您可以使用NOW()
$query = "INSERT INTO `time` (`now`) VALUES (NOW())";
答案 1 :(得分:0)
尝试使用此格式
$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";
OR
$query = "INSERT INTO time (now) VALUES(NOW())";
答案 2 :(得分:0)
<?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";
?>
答案 3 :(得分:0)
从
更改$ 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')."')";