简体   繁体   English

如何修复“未捕获的 PDOException:...从字符串转换日期和/或时间时转换失败”?

[英]How to fix "Uncaught PDOException: ...Conversion failed when converting date and/or time from character string"?

I'm developing an event calendar using full-Calendar.我正在使用 full-Calendar 开发事件日历。 I'm getting the above error after an event has been dropped on the calendar.在日历上删除事件后,我收到上述错误。 It seems that the date format needs to be converted, I'm not sure exactly how should I do that.似乎需要转换日期格式,我不确定我应该怎么做。 In one of my previous questions I was suggested that something like yyyy-mm-dd hh:mm would work better and that should be done using momentJs.在我之前的一个问题中,有人建议我使用像 yyyy-mm-dd hh:mm 这样的东西会更好,并且应该使用 momentJs 来完成。

As I'm very new to js, I'm not sure how and what exactly should I change in my code.由于我对 js 非常陌生,因此我不确定我应该如何更改代码以及应该更改哪些内容。

Here's the code:这是代码:

   document.addEventListener('DOMContentLoaded', function() {
  var Calendar = FullCalendar.Calendar;
  var Draggable = FullCalendarInteraction.Draggable;

  var containerEl = document.getElementById('external-events');
  var calendarEl = document.getElementById('calendar');
  var checkbox = document.getElementById('drop-remove');


 new Draggable(containerEl, {
    itemSelector: '.fc-event',
    eventData: function(eventEl) {
    return {
      title: eventEl.innerText
     };
   }
  });

 var calendar = new Calendar(calendarEl, {
 plugins: ['interaction', 'dayGrid', 'timeGrid'],
 header: {
   left: 'prev,next today',
  center: 'title',
  right: 'dayGridMonth,timeGridWeek,timeGridDay'
},
editable: true,
droppable: true,
eventReceive: function(info) {

  //get the bits of data we want to send into a simple object
  var eventData = {
    title: info.event.title,
    start: info.event.start,
    end: info.event.end
  };
  console.log(eventData);
  //send the data via an AJAX POST request, and log any response which comes from the server
  fetch('add_event.php', {
      method: 'POST',
      headers: {
        'Accept': 'application/json'
      },
      body: encodeFormData(eventData)
    })
    .then(response => console.log(response))
    .catch(error => console.log(error));
    }
 });
  calendar.render();
});

const encodeFormData = (data) => {
  var form_data = new FormData();

for (var key in data) {
   form_data.append(key, data[key]);
  }
  return form_data;
}

index.php索引.php

<link href='https://unpkg.com/@fullcalendar/core@4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/@fullcalendar/daygrid@4.4.0/main.min.css' rel='stylesheet' />
<link href='https://unpkg.com/@fullcalendar/timegrid@4.4.0/main.min.css' rel='stylesheet' />
<script src='https://unpkg.com/@fullcalendar/core@4.4.0/main.min.js'></script>
<script src='https://unpkg.com/@fullcalendar/interaction@4.4.0/main.min.js'></script>
<script src='https://unpkg.com/@fullcalendar/daygrid@4.4.0/main.min.js'></script>
<script src='https://unpkg.com/@fullcalendar/timegrid@4.4.0/main.min.js'></script>


<div id='external-events'>
  <p>
<strong>Draggable Events</strong>
 </p>
 <div class='fc-event'>My Event 1</div>
  <div class='fc-event'>My Event 2</div>
  <div class='fc-event'>My Event 3</div>
  <div class='fc-event'>My Event 4</div>
  <div class='fc-event'>My Event 5</div>
  <p>
    <input type='checkbox' id='drop-remove' />
    <label for='drop-remove'>remove after drop</label>
  </p>
</div>
<div id='calendar-container'>

add_event.php add_event.php

require 'connection.php';
    $title = $_POST['title'];
    $start = $_POST['start'];
    $end = $_POST['end'];
        $conn = DB::databaseConnection();
       $conn->beginTransaction();
$sqlInsert = "INSERT INTO Events ( title, start, [end]) VALUES ( :title, :start, :end)";
    $stmt = $conn->prepare($sqlInsert);
    $stmt->bindParam(':title', $title);
    $stmt->bindParam(':start', $start);
    $stmt->bindParam(':end', $end);
    if ($stmt->execute()) {
            $conn->commit();
            return true;
        } else {
            $conn->rollback();
            return false;
        }

    ?>

The event details needs to be saved in a ms-sql database.事件详细信息需要保存在 ms-sql 数据库中。 The function for that is in the add_event.php file.该函数位于 add_event.php 文件中。 Please let me know if you would like to see the code for that too, will post that too.如果您也想查看该代码,请告诉我,我也会发布该代码。

If you're using momentJS, and, assuming you've included the momentJS file in your page already, then:如果您使用的是 momentJS,并且假设您已经在页面中包含了 momentJS 文件,那么:

start:moment(info.event.start).format("YYYY-MM-DD HH:mm"),
end:moment(info.event.start).format("YYYY-MM-DD HH:mm" )

should do the trick.应该做的伎俩。 See https://momentjs.com/docs/#/displaying/format/ for details.有关详细信息,请参阅https://momentjs.com/docs/#/displaying/format/

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

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