【问题标题】:Angular - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime valueAngular - SQLSTATE [22007]:日期时间格式无效:1292 日期时间值不正确
【发布时间】:2019-10-02 11:38:23
【问题描述】:

我正在开发一个客户端门户应用程序。 Laravel-5.8 是后端,Angular-7 是前端。我有这张桌子:

表格

CREATE TABLE `client_quotes` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `last_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `phone` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `business_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `truck_required` int(10) UNSIGNED NOT NULL,
  `truck_type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT 'Flatbed 20 Ton',
  `quote_origin` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `quote_destination` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `commodity` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `loading_date` datetime DEFAULT NULL,
  `comment` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ApiController.php

    public function createClientQuote(Request $request) {
        $request->validate([
                    'first_name' => 'required',
                    'last_name' => 'required',
                    'email' => 'required|email',
                    //'email' => 'required|email|unique:users|max:255',
                    'phone' => 'required|max:14',
                    'business_name' => 'required',
                    'truck_type' => 'required',
                    'truck_required' => 'required',
                    'quote_origin' => 'required',
                    'quote_destination' => 'required',
                    'commodity' => 'required',  
                    // 'weight' => 'required',
                    'loading_date' => 'date|required' 
        ]);
        $clientquote = new ClientQuote;
        $clientquote->first_name=$request->get('first_name');
        $clientquote->last_name=$request->get('last_name');
        $clientquote->email=$request->get('email');
        $clientquote->phone=$request->get('phone');
        $clientquote->business_name=$request->get('business_name');
        $clientquote->truck_type=$request->get('truck_type');
        $clientquote->truck_required=$request->get('truck_required');
        $clientquote->quote_origin=$request->get('quote_origin');
        $clientquote->quote_destination=$request->get('quote_destination');
        $clientquote->commodity=$request->get('commodity');
        $loading_date=date_create($request->get('loading_date'));
        $format = date_format($loading_date,"Y-m-d H:i:s");
        $clientquote->loading_date = strtotime($format);
        $clientquote->save();

        return response()->json([
            'message' => 'Quote Successfully Sent!'
        ], 201);
    }

在上面的 ApiController.php 中,我尝试使用这些格式设置 loading_date:

    $loading_date=date_create($request->get('loading_date'));
    $format = date_format($loading_date,"Y-m-d H:i:s");
    $clientquote->loading_date = strtotime($format);

client-quote.component.html

    <form class="form-clientquote" #clientquoteForm=ngForm (ngSubmit)="onSubmit()">
    <div class="row">
      <div class="col-xs-12">

        <div class="col-xs-6">
        <label for="loading_date">Loading Date<span style="color:red;"> *</span></label>
        <div class="input-group date" style="width: 100%;" >
        <mat-form-field>
        <input matInput [matDatepicker] = "picker" placeholder = "Choose a date" name="loading_date" [ngModel]="form.loading_date | date: 'dd/MM/yyyy'" (ngModelChange)=" form.loading_date= $event"  #loading_date="ngModel" [ngClass]="{'is-invalid' : loading_date.invalid && ((loading_date.dirty || loading_date.touched) || clientquoteForm.submitted)}"   required>
          <mat-datepicker-toggle matSuffix [for] = "picker"></mat-datepicker-toggle>
           <mat-datepicker #picker></mat-datepicker>
           </mat-form-field>
           <div class="form-feedback" *ngIf="loading_date.invalid && ((loading_date.dirty || loading_date.touched) || clientquoteForm.submitted)">
              <div style="color:red;" *ngIf="loading_date.errors?.required"class="alert alert-danger">Loading Date is required.</div>
              </div>
           </div>
         </div>
     </div>
   </div>

   <div class="row">
     <div class="col-xs-12">
       <div class="col-xs-12">
       <div class="btn-group">
          <button style="margin:5px" (keyup.enter)="onSubmit()" type="submit" class="btn btn-success" awNextStep> Get A Quote</button>
       </div>
       </div>
     </div>
   </div>
   </form>

当我点击提交按钮时,我希望应用程序保存到数据库中。我收到了这个错误:

"SQLSTATE[22007]: 无效的日期时间格式: 1292 错误的日期时间值: '1572307200' 列clientportal.client_quotes.loading_date 在第 1 行 (SQL: 插入到client_quotes (first_name, last_name, email, phone, business_name, truck_type, truck_required, quote_origin, quote_destination, commodity, commodity, updated_at, loading_date, @98765434 Ademola, Adebila, decking@gmail.com, 09048614390, JOKA, I Don't know, 2, Ajegunle, Ebutt, Rice, 1572307200, 2019-10-02 11:15:42, 2019-10-02 11:15: 42))"

我认为错误与加载日期有关。我该如何解决?

【问题讨论】:

  • 尝试转储$request-&gt;get('loading_date')date_format($loading_date,"Y-m-d H:i:s")strtotime($format) 以查看日期格式。它似乎在某处转换为时间戳

标签: angular laravel


【解决方案1】:

datetime 列需要 Y-m-d H:i:s 格式的值。但是您正试图插入一个 UNIX 时间戳,例如 1572307200。所以这就是问题所在。如果您想要一个 UNIX 时间戳,请将列类型设置为 varchar 或保持原样。您可以稍后将其转换为 UNIX 时间戳。所以现在删除strtotime 函数。

$loading_date=date_create($request->get('loading_date'));
$format = date_format($loading_date,"Y-m-d H:i:s");
$clientquote->loading_date = $format;

【讨论】:

    【解决方案2】:

    查看错误代码,loading_date 属性的值似乎是 unix 纪元的字符串表示形式,而不是日期时间对象。

    作为修复,我建议您迁移 SQL loading_date 列以使用字符串值,然后将 fe 请求值转换为 ISO dateTimeString。以后如果需要使用 dateTime-/Carbon-object 原生的方法,很容易将 db 中的字符串解析为 dateTime-/Carbon-object。

    这样做的另一个好处是您也可以存储时间本地化数据,并且您不必担心将来对库的更改会破坏您的代码。

    【讨论】:

      猜你喜欢
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-19
      • 2022-06-20
      • 2017-07-18
      相关资源
      最近更新 更多