php - HTML 日期时间 到 PHP 到 MySQL

标签 php html mysql datetime

我正在使用 HTML、PHP 和 MySQL 制作一个待办事项列表网页,但我无法将日期时间提交到数据库:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Sign Controller</title>
  <link rel="stylesheet" href="/css/style.css" />
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script src="update.js"></script>
</head>
<body>
  <!--MAIN CONTENT HERE-->
  <div class="container">
    <form id="controller" action="php/update.php" method="post">
      <h3>Sign display controller</h3>
      <h4>Event Controller</h4>
      <fieldset>
        <input placeholder="sign ID" type="text" name="sign_Name" tabindex="1" required autofocus/>
      </fieldset>
      <h5>Event Start</h5>
      <fieldset>
        <input type="datetime-local" name="time_eventStart" tabindex="2" required/>
      </fieldset>
      <h5>Event End</h5>
      <fieldset>
        <input type="datetime-local" name="time_eventEnd" tabindex="3" required/>
      </fieldset>
      <fieldset>
        <input placeholder="Event source" type="text" name="event_Source" tabindex="4"/>
      </fieldset>
      <fieldset>
        <input placeholder="Idle source" type="text" name="idle_Source" tabindex="5"/>
      </fieldset>
      <button type="submit">Update</button>
    </form>
  </div>
</body>
</html>

PHP:

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "projectSS";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sign_Name = $_POST["sign_Name"];
$event_Source = $_POST["event_Source"];
$idle_Source = $_POST["idle_Source"];
$time_eventStart = $_POST["time_eventStart"];
$time_eventEnd = $_POST["time_eventEnd"];

$sql = "INSERT INTO display (sign_Name,event_timeStart,event_timeEnd,event_Source,idle_Source)
VALUES ('$sign_Name','$time_eventStart','$time_eventEnd','$event_Source','$idle_Source')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

我很难处理 datetime-local 类型,而且我是编程新手。

最佳答案

只需按照 mysql 标准更改日期时间格式

如果您的列数据类型是下面使用的日期

$time_eventStart = date('Y-m-d',strtotime($_POST["time_eventStart"]));
$time_eventEnd = date('Y-m-d',strtotime($_POST["time_eventEnd"]));

如果您的列数据类型是下面使用的日期时间

$time_eventStart = date('Y-m-d H:i:s',strtotime($_POST["time_eventStart"]));
$time_eventEnd = date('Y-m-d H:i:s',strtotime($_POST["time_eventEnd"]));

关于php - HTML 日期时间 到 PHP 到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45050573/

相关文章:

php - 从复选框设置 cookie 以跳过登陆页面

php - codeigniter 中的这个数据库错误是什么

javascript - 更新 JavaScript 函数

mysql - 当 “\n” 作为文本嵌入时格式化 SHOW ENGINE INNODB STATUS 输出

mysql - 为什么在构建事件源时应该扇出

javascript - 将数据从 Excel CSV 读取到 html 页面并搜索

javascript - 自动提交php表单

html - 3 个相邻的 100% 大小的 div,另一个作为包含流畅照片的背景

android - 仅在安装时在 phonegap android 应用程序中创建数据库

php - fsockopen 错误我想跳过