简体   繁体   English

如何添加提交的时间表格

[英]How to add the time form was submitted

I have this page below, which is the action when you submit a form. 我在下面有此页面,这是您提交表单时的操作。 I am wondering how you can add a time function to this, to add the time that the form was submitted to my database table in the row "time". 我想知道如何向其中添加时间函数,以在“时间”行中添加表单提交到我的数据库表的时间。 I have tried numerous things and can't get them to work :/ 我尝试了很多事情,但无法使它们起作用:/

<?php
require 'db/connect.php';

if(isset($_POST['submitted']))
{

    $player = $_POST['inputWho'];
    $offense = $_POST['inputWhat'];
    $where = $_POST['inputPlace'];
    $proof = $_POST['inputProof'];
    $comments = $_POST['inputComments'];

    $sqlinsert = "INSERT INTO offenses (player, reason, place, proof, comments) VALUES  ('$player', '$offense', '$where', '$proof', '$comments')";

    if (!mysqli_query($db, $sqlinsert)) {
        die('Error inserting new record.'. "<br />". mysqli_error($db));
    } //end of nested if

} //end of if

header("Location: success.php");

?>

The way I usually approach this problem is to add a column to the table that looks like this: 我通常解决此问题的方法是在表中添加如下所示的列:

created_time datetime default current_timestamp

You don't even have to include it in your INSERT statement, it will get set automatically when you create the row. 您甚至不必将其包含在INSERT语句中,在创建行时它将自动进行设置。


EDIT: The above statement only works with type TIMESTAMP, not DATETIME. 编辑:上面的语句仅适用于TIMESTAMP类型,不适用于DATETIME。 (Many people are probably still on MySQL 5.5 or earlier at present.) This is discussed in the manual here: http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html (当前许多人可能仍在使用MySQL 5.5或更早版本。)这在手册中进行了讨论: http : //dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

You can avoid this version problem by using Skywalker's answer . 您可以使用Skywalker的answer来避免此版本问题。 (Or by not using MySQL.) (或者不使用MySQL。)


If you also need the column set when you update , that's a different issue. 如果在更新时还需要设置列集,那就是另一个问题。

I find it's generally preferable to get the time from inside the database rather than supplying one from the web server. 我发现通常最好是从数据库内部获取时间,而不是从Web服务器提供时间。 This avoids any problems with your web server time getting slightly out of sync with your database server. 这样可以避免Web服务器时间与数据库服务器稍微不同步的任何问题。 (You may even have multiple web servers that are out of sync with each other.) (您甚至可能有多个彼此不同步的Web服务器。)

Make your sql like this: 使您的sql像这样:

$sqlinsert = "INSERT INTO offenses (player, reason, place, proof, comments, time) VALUES  ('$player', '$offense', '$where', '$proof', '$comments', NOW())";

Make use of MySQL function NOW(). 利用MySQL函数NOW()。

Try this: 尝试这个:

date_default_timezone_set('America/Los_Angeles');
$dateEnquiry = date("Y-m-d H:i:s", time());

$sqlinsert = "INSERT INTO offenses (player, reason, place, proof, comments, time) VALUES  ('$player', '$offense', '$where', '$proof', '$comments', '$dateEnquiry')";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM