简体   繁体   English

SQLSTATE[22007]:无效的日期时间格式:1366 不正确的整数值:Laravel 中的“column_name”

[英]SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'column_name' in Laravel

Laravel Multiple Data Insert Error Laravel 多数据插入错误

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column 'unit_id' at row 2 (SQL: insert into product_prices ( created_at , product_id , unit_id , updated_at ) values (2016-12-06 06:56:01, 27, 1,2016-12-06 06:56:01), (2016-12-06 06:56:01,27, , 2016-12-06 06:56:01)) SQLSTATE[22007]:无效的日期时间格式:1366 不正确的整数值:第 2 行的列“unit_id”的“”(SQL:插入product_pricescreated_atproduct_idunit_idupdated_at )值(2016-12-06 06:56: 01, 27, 1,2016-12-06 06:56:01), (2016-12-06 06:56:01,27, , 2016-12-06 06:56:01))

But my unit_id field in nullable();但是我在nullable(); unit_id字段nullable(); Please someone help me Here column_name=unit_id请有人帮我这里column_name=unit_id

null is different than not existend. null 与不存在不同。 If you want to set null as a value you have to write it in your query:如果要将 null 设置为值,则必须在查询中写入它:

 ... ('2016-12-06 06:56:01',27, null, '2016-12-06 06:56:01'))

Also the datetime format is wrong.日期时间格式也是错误的。 You have to enter it as a string.您必须将其作为字符串输入。

Set 0 for unit_id before query, if it null/empty .在查询之前为unit_id设置 0,如果它为null/empty See example:见示例:

if(!isset($unit_id) || empty($unit_id)) $unit_id = 0;
.
.
//insert query rest code

Just had the same issue and in my case it was a silly mistake in my controller.刚刚遇到了同样的问题,就我而言,这是我的控制器中的一个愚蠢的错误。

What I did was I returned the whole object instead of just the id, like so:我所做的是我返回了整个对象,而不仅仅是 id,如下所示:

  public function store($id, Request $request) {

    $post = Post::find($id);

    $comment = new Comment;
    $comment->text = $request->comment;
    $comment->post_id = $post; <--- HERE IS THE MISTAKE 
    $comment->post_id = $post->id; <--- HERE IS THE FIX
    $comment->user_id = Auth::id();

    $comment->save();

    return back();

  }

You are using unit_id is it referenced with units( id ) ?.您正在使用 unit_id 是否引用了单位( id )?。 You are inputing a empty value for a foregin key referenced column.use null and not ''您正在为外键引用的列输入一个空值。使用null而不是''

 insert into product_prices (created_at, product_id, unit_id, updated_at)
  values (2016-12-06 06:56:01, 27, 1,2016-12-06 06:56:01), 
         (2016-12-06 06:56:01,27,null, 2016-12-06 06:56:01);

I was encountering the same error.我遇到了同样的错误。 update_at and created_at are automatically created by migration. update_at 和 created_at 是由迁移自动创建的。 Backup your model and delete it.备份您的模型并将其删除。 Then create a new Model using php artisan use php artisan make:model Model Name -m then in your migration table add the fields you need eg.然后使用 php artisan use php artisan make:model Model Name -m 创建一个新模型,然后在您的迁移表中添加您需要的字段,例如。

 public function up()
{
    Schema::create('flights', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('airline');
        $table->timestamps();
    });
}

then do the migration php artisan migrate然后进行迁移 php artisan migrate

i can solve it, check your payload when sent request put or post to your api我可以解决它,在发送请求时检查您的有效负载放置或发布到您的 api

i should send this json:我应该发送这个json:

{
...,
"item":1
}

but i sent this.但我发送了这个。 this is my mistake这是我的错误

{
...,
"item":[{"id"=1}]
}

or { ..., "item":[1] }或 { ..., "item":[1] }

my code in laravel is this:我在 Laravel 中的代码是这样的:

   $item= Item::find($request->input('item'));
   $client->item()->associate($item);

Using the intval() function can solve the problem quite well.使用intval()函数可以很好地解决问题。

intval($unit_id);

This code will return 0 if:如果出现以下情况,此代码将返回0

  • $unit_id is not set. $unit_id未设置。
  • $unit_id is string and not alphanumeric (eg 'abc' ). $unit_id是字符串而不是字母数字(例如'abc' )。
  • $unit_id is NULL $unit_idNULL

If $unit_id is alphanumeric string (eg '10' ), it'll return the number.如果$unit_id是字母数字字符串(例如'10' ),它将返回数字。

in my case it solved by using validator in insert() function pass your post name to this在我的情况下,它通过在 insert() 函数中使用验证器来解决,将您的帖子名称传递给这个

function if_set($v) if (isset($_POST[$v]) and $_POST[$v] != '') $data_to_db[$v] = $_POST[$v];函数 if_set($v) if (isset($_POST[$v]) 和 $_POST[$v] != '') $data_to_db[$v] = $_POST[$v];

暂无
暂无

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

相关问题 Laravel 8:SQLSTATE[22007]:无效的日期时间格式:1366 不正确的 integer 值 - Laravel 8: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value SQLSTATE [22007]:无效的日期时间格式:1366不正确的整数值 - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value SQLSTATE [22007]:无效的日期时间格式:1366 不正确的 integer 值:问题 - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: problem SQLSTATE [22007]:无效的日期时间格式:1366错误的整数值:行1的列&#39;infraction_id&#39;的&#39;[“ 1”,“ 3”,“ 66”,“ 68”]&#39; - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[“1”,“3”,“66”,“68”]' for column 'infraction_id' at row 1 SQLSTATE [22007]:无效的日期时间格式:1366 不正确的 integer 值:“isVisible”列的“true” - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'true' for column 'isVisible' QLSTATE [22007]:无效的日期时间格式:1366 不正确的 integer 值 laravel - QLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value laravel laravel SQLSTATE [22007]:无效的日期时间格式:1366不正确的十进制值:&#39;buy_amt * 0.00097812555575316&#39;为行1上的价格列错误 - laravel SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect decimal value: 'buy_amt * 0.00097812555575316' for column 'price' at row 1 error SQLSTATE[22007]:无效的日期时间格式:1366 字符串值不正确,html_entity_decode function - SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value, html_entity_decode function Laravel 将时间戳保存为 DateTime - SQLSTATE[22007]:日期时间格式无效:1292 日期时间值不正确: - Timestamp being saved as DateTime by Laravel - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: SQLSTATE[22007]:无效的日期时间格式:1292 日期时间值不正确 - SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM