SQLSTATE [22007]:无效的日期时间格式:1292日期时间值不正确

时间:2017-07-06 15:02:27

标签: javascript php mysql laravel fullcalendar

我正在将完整的日历API实现到我的项目中。我在尝试保存事件时遇到问题,我不断收到以下错误:

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2017-07-1415:41:03' for column 'start' at row 1 (SQL: insert into `event3s` (`title`, `start`, `end`, `color`) values (New event, 2017-07-1415:41:03, , #9e1515))

我已经在phpmyadmin中更改到了正确的时间(时区),但我仍然遇到了同样的错误。

这是我在phpmyadmin中的表格: [![在此处输入图像说明] [1]] [1]

这是calendar.blade.php类:

<!doctype html>
<html lang="{{ config('calendar.locale') }}">
<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <title>Full Calendar</title>

    {!! Html::style('vendor2/seguce92/bootstrap/css/bootstrap.min.css') !!}
    {!! Html::style('vendor2/seguce92/fullcalendar/fullcalendar.min.css') !!}
    {!! Html::style('vendor2/seguce92/bootstrap-datetimepicker/css/bootstrap-material-datetimepicker.css') !!}
    {!! Html::style('vendor2/seguce92/bootstrap-colorpicker/css/bootstrap-colorpicker.min.css') !!}

</head>
<div id='app'></div>
<div class='container'></div>

{{ Form::open(['route' => 'user.events.store', 'method' => 'post', 'role' => 'form']) }}
<div id="responsive-modal" class="modal fade" tabindex="-1" data-backdrop="static"->

    <div class="modal-dialog">
        <div class="modal-content">

        <div class="modal-header">
        <h4>Registration of new Event</h4>

        </div>

        <div class="modal-body">

            <div class="form-group">
                {{Form::label('title','Date Start') }}
                {{Form::text('title',old('title'),['class' => 'form-control'])}}
            </div>

        <div class="form-group">
            {{Form::label('date_start','Date Start') }}
            {{Form::text('date_start',old('date_start'),['class' => 'form-control','readonly'=>'true'])}}
        </div>

            <div class="form-group">
                {{Form::label('time_start','Time Start') }}
                {{Form::text('time_start',old('time_start'),['class' => 'form-control'])}}
            </div>

            <div class="form-group">
                {{Form::label('date_end','Time Finish') }}
                {{Form::text('date_end',old('date_end'),['class' => 'form-control'])}}
            </div>

            <div class="form-group">
                {{Form::label('color','COLOR') }}
                <div class="input-group colorpicker">
                    {{ Form::text('color',old('color'),['class' => 'form-control']) }}
                    <span class ="input-group-addon">

                        <i></i>
                    </span>
                </div>
            </div>
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            {!! Form::submit('Save',['class' => 'btn-btn-success'])!!}

        </div>
    </div>
    </div>

</div>
{{Form::close()}}
<div id='calendar'></div>

</body>

{!! Html::script('vendor2/seguce92/jquery.min.js') !!}
{!! Html::script('vendor2/seguce92/bootstrap/js/bootstrap.min.js') !!}
{!! Html::script('vendor2/seguce92/fullcalendar/lib/moment.min.js') !!}
{!! Html::script('vendor2/seguce92/fullcalendar/fullcalendar.min.js') !!}
{!! Html::script('vendor2/seguce92/bootstrap-datetimepicker/js/bootstrap-material-datetimepicker.js') !!}
{!! Html::script('vendor2/seguce92/bootstrap-colorpicker/js/bootstrap-colorpicker.min.js') !!}
    <script type="text/javascript">
    var BASEURL="{{url('/')}}";
        $(document).ready(function() {

            $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,basicWeek,basicDay'
                },

                navLinks: true, // can click day/week names to navigate views
                editable: true,
                selectable:true,
                selectHelper:true,

                select:function(start){
                    start = moment(start.format());

                    $('#date_start').val(start.format('YYYY-MM-DD'));
                    $('#responsive-modal').modal('show');
                },
                events: BASEURL+'/events'

            });

        });

        $('.colorpicker').colorpicker();

        $('#time_start').bootstrapMaterialDatePicker({

                date:false,
                shortTime: false,
                format: 'HH:mm:ss'
        });

    $('#date_end').bootstrapMaterialDatePicker({

        date:true,
        shortTime: false,
        format: 'YYYY-MM-DD'
    });

有一点需要注意,我必须通过phpmyadmin创建表,我无法从代码中迁移表。

此外,我还尝试将YYYY-MM-DD更改为Y-m-d H:i:s

这是我的迁移类:

class CreateEvent3sTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('event3s', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->datetime('start');
            $table->datetime('end');
            $table->string('color', 7);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('event3s');
    }
}

这是我的控制器,保存过程即将发生:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Event3;
class EventsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Event3::get(['title','start','end','color']);

       return Response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $event = new Event3();
       $event->title = $request->title;
       $event->start = $request->date_start .''. $request->time_start;
       $event->end = $request->data_end;
       $event->color = $request->color;
        $event->save();

        return redirect('/');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

3 个答案:

答案 0 :(得分:1)

仔细查看错误消息...datetime value: '2017-07-1415:41:03' for...日期和时间值不以空格分隔。

查看实际尝试写入数据库的php脚本非常有用

答案 1 :(得分:1)

问题在于store()方法:

$event->start = $request->date_start .''. $request->time_start;

正如您所看到的,date_starttime_start之间没有空格。只需将其更改为

即可
$event->start = $request->date_start .' '. $request->time_start;

date_end也存在问题。改变这一行:

$event->end = $request->data_end;

$event->end = $request->date_end;

它应该在没有错误的情况下进入您的数据库!

答案 2 :(得分:0)

MySQL在DATETIME中检索并显示YYYY-MM-DD HH:MM:SS个值。您的datetime值似乎缺少一个空格,而应该是'2017-07-14 15:41:03'而不是