php 如何在我的 mysql 表中插入时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37616760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert timestamp into my mysql table
提问by johnbumble
In the mysql table I have a field called date its type is called timestamp and the default is CURRENT_TIMESTAMP. However, if I leave the field blank in mysql I get an error. When I try to insert something into it like time() I receive the date as 0000-00-00 00:00:00.
在 mysql 表中,我有一个名为 date 的字段,其类型称为时间戳,默认值为 CURRENT_TIMESTAMP。但是,如果我在 mysql 中将该字段留空,则会出现错误。当我尝试像 time() 这样插入一些东西时,我收到的日期为 0000-00-00 00:00:00。
<?php
$name = "";
$email = "";
$subject = "";
$comments = "";
$nameError = "";
$emailError = "";
$subjectError = "";
$x = 5;
function filterData($data)
{
$data = htmlspecialchars($data);
$data = stripslashes($data);
return $data;
}
$connection = mysql_connect('host', 'user', 'pass');
if (!$connection) {
die('Could not connect: ' . mysql_error());
}
$select_database = mysql_select_db("contact");
if (!$select_database) {
echo "could not select database " . mysql_error();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//handles the name
$name = filterData($_POST["name"]);
if (empty($name)) {
$nameError = "please don't leave the name field blank";
}
//handles the email
$email = filterData($_POST["email"]);
if (empty($email)) {
$emailError = "please don't leave the email field blank";
}
//handles the subject
$subject = filterData($_POST["subject"]);
if (empty($subject)) {
$subjectError = "please don't leave this field blank";
}
$comments = filterData($_POST["comments"]);
}
$insertation = "INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', '', '$comments')";
$insertationQuery = mysql_query($insertation, $connection);
if (!$insertationQuery) {
echo "Could not process your information " . mysql_error();
} else {
echo "Thank you for submitting the information";
}
?>
回答by shamsup
In addition to checking your table setup to confirm that the field is set to NOT NULL
with a default of CURRENT_TIMESTAMP
, you can insert date/time values from PHP by writing them in a string format compatible with MySQL.
除了检查您的表设置以确认该字段设置为NOT NULL
默认值之外CURRENT_TIMESTAMP
,您还可以通过以与 MySQL 兼容的字符串格式编写日期/时间值来插入 PHP 中的日期/时间值。
$timestamp = date("Y-m-d H:i:s");
This will give you the current date and time in a string format that you can insert into MySQL.
这将以字符串格式为您提供当前日期和时间,您可以将其插入到 MySQL 中。
回答by SonDang
Please try CURRENT_TIME()
or now()
functions
请尝试CURRENT_TIME()
或now()
功能
"INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', NOW(), '$comments')"
OR
或者
"INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', CURRENT_TIME(), '$comments')"
OR you could try with PHP date
function here:
或者你可以在这里尝试使用 PHPdate
函数:
$date = date("Y-m-d H:i:s");
回答by Hamza Zafeer
$insertation = "INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', CURRENT_TIMESTAMP, '$comments')";
You can use this Query. CURRENT_TIMESTAMP
您可以使用此查询。CURRENT_TIMESTAMP
回答by Abu Shoeb
You do not need to insert the current timestamp manually as MySQL
provides this facility to store it automatically. When the MySQL
table is created, simply do this:
您不需要手动插入当前时间戳,因为MySQL
此工具提供了自动存储它的功能。当MySQL
创建表,只要做到这一点:
- select
TIMESTAMP
as your column type - set the
Default
value toCURRENT_TIMESTAMP
- then just
insert
any rows into the table without inserting any values for thetime
column
- 选择
TIMESTAMP
作为您的列类型 - 将
Default
值设置为CURRENT_TIMESTAMP
- 然后只是
insert
表中的任何行而不为time
列插入任何值
You'll see the current timestamp
is automatically inserted when you insert a row. Please see the attached picture.
回答by salvatore
The DEFAULT
value of a column in MySql is used only if it isn't provided a value for that column.
So if you
的DEFAULT
,如果它不提供用于该列的值MySQL中的列的值仅使用。所以如果你
INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', '', '$comments')
You are not using the DEFAULT
value for the column date
, but you are providing an empty string, so you get an error, because you can't store an empty string in a DATETIME
column.
The same thing apply if you use NULL
, because again NULL
is a value.
However, if you remove the column from the list of the column you are inserting, MySql will use the DEFAULT
value specified for that column (or the data type default one)
您没有使用DEFAULT
column的值date
,但是您提供了一个空字符串,因此您会收到错误消息,因为您无法在DATETIME
列中存储空字符串。如果您使用NULL
,同样适用,因为再次NULL
是一个值。但是,如果从要插入的列的列表中删除该列,MySql 将使用DEFAULT
为该列指定的值(或数据类型默认值)
回答by MikeGsus
You can try wiht TIMESTAMP(curdate(), curtime())for use the current time.
您可以尝试使用TIMESTAMP(curdate(), curtime())来使用当前时间。