【问题标题】:insert javascript date and time objects to database将javascript日期和时间对象插入数据库
【发布时间】:2018-07-28 15:11:50
【问题描述】:

我的表单上有日期和时间显示,现在我的问题是如何 我可以将日期和时间的值插入数据库,希望你能帮助我。谢谢。

这是我的日期和时间代码:

<div class="row" style="margin-left:300px;">
             <label>Date:</label>  
             <span id="date" name="calendar"></span>
             </div>
              <div class="row" style="margin-left:300px;">
             <label>Time:</label> 
            <span id="clock" name="time"></span>
             </div>  

我的php代码:

<?php
include_once ('connection.php');
$calendar=$_POST["calendar"];
$calendar=date("Y-m-d",$calendar);
$time=$_POST["time"];
$time=date("h-i-s",$time);


        $InsertSql = "INSERT INTO test2(date,timein) VALUES ('$calendar','$time')";
          $res = mysqli_query($conn, $InsertSql);

?>

日期和时间的javascript:

<script>
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var n = new Date();
var y = n.getFullYear();
var m = n.getMonth();
var d = n.getDate();
document.getElementById("date").innerHTML = months[m] + " " + d + " " + y;
</script>

<script>
(function () {

  var clockElement = document.getElementById( "clock" );

  function updateClock ( clock ) {
    clock.innerHTML = new Date().toLocaleTimeString();
  }

  setInterval(function () {
      updateClock( clockElement );
  }, 1000);

}());
</script>

更新了我的问题。

here is my form

error

【问题讨论】:

  • 到底是什么问题?
  • 关于如何将表单上显示的日期和时间的值插入数据库
  • 如何将表单中的任何其他值插入到数据库中?
  • 但是有一件事……永远不要只将变量放入 SQL 查询中——这是引发 SQL 注入攻击的最快方法。使用准备好的语句
  • 欢迎来到stackoverflow。请包括一个清晰的问题描述,如果可能的话,一些最小的代码。也告诉我们你尝试了什么。如需指导,请查看how to ask 页面和how to create a minimal example

标签: php jquery html mysql


【解决方案1】:

如何将它存储在一个变量中。像这样的

<form action="" method="POST">
    <button name="submit" class="submit">Send</button>
</form>
<?php
if(isset($_POST['submit']))
{
$calendar=date("Y-m-d");
$time=date("H:i:s");
$InsertSql = "INSERT INTO test2(date,timein) VALUES ('$calendar','$time')";
          $res = mysqli_query($conn, $InsertSql);
}        

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-06
    • 2015-12-31
    • 2018-05-18
    • 1970-01-01
    • 2018-12-17
    • 2012-05-01
    相关资源
    最近更新 更多