-1

我正在尝试使用简单查询向我的MySql数据库发送时间。这是代码。localhostPHP

在 HTML 方面:

    <form action = "Timecard.php"
method = "get">
<fieldset>
    <label>Enter employee number:</label>
    <input type = "text"
    name = "EmployeeNumber" />

    <button type = "submit" name = "submit" value = "ClockIn">        
        Clock In
    </button>
    <button type = "submit" name = "submit" value = "ClockOut">
        Clock Out
    </button>
</fieldset>
</form>

And the PHP:
<?php
require ("conn.php");

$filled = true;

$EmpNum = $_REQUEST["EmployeeNumber"];

if ($EmpNum == "") {
    $filled = false;

}

if ($filled == false) {
    print "Must enter a valid employee number to log in. Click <a href = 'http://localhost/Employee Timecard/Timecard.html'>HERE</a> to return to the login screen";
} else {
    $timestamp = $_SERVER['REQUEST_TIME'];
    $date = date('Y-m-d', $timestamp);
    $time = sprintf(date('h-i-s', $timestamp));
    print $timestamp . "<br>";

    print $EmpNum . " has just logged ";

    if (($SubmitButton = $_REQUEST["submit"]) == "ClockIn") {
        print "in on " . $date . " at " . $time;
        $query = sprintf("INSERT INTO timestamp(EmployeeNumber, Date, TimeIn, TimeOut) VALUES (".$EmpNum.",'".$date."','".$time."','')");
        print $query;
        $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
        print "checkpoint 2";
    } else if (($SubmittButton = $_REQUEST["submit"]) == "ClockOut") {
        print "out on " . $date . " at " . $time;
        $query = sprintf("INSERT INTO timestamp(EmployeeNumber, Date, TimeIn, TimeOut) VALUES (".$EmpNum.",'".$date."','',".$time.")");
        print $query;
        $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
        print "checkpoint 2";
    }
}
print "<br>checkpoint 3";
?>

当我提交到数据库时,日期正常但时间不正确 - 类似于-00:00:4700:00:05.数据库中字段的类型设置为"time"。为什么我会得到这些结果?它必须很简单,但它让我发疯。

4

1 回答 1

1

您可以在这里简单地使用date()

 $query = "INSERT INTO timestamp(EmployeeNumber, Date, TimeIn, TimeOut) 
           VALUES (".$EmpNum.",'".$date."','','".date('H:i:s')."')";
 print $query;
于 2013-08-05T05:34:11.120 回答