SQLSTATE [01000]:警告:1265列的数据被截断

时间:2017-12-28 03:25:59

标签: mysql sql laravel request

SQLSTATE [01000]:警告:1265第1行的“pay_totals”列数据被截断

public function order(Request $req){
        $order = new Order;
        $order->pay_number = $req->checkout_number;
        $order->pay_totals = $req->checkout_total;
        $order->save();
        return redirect(route('pay'))->with('message','Sending infomation successfully');
    }

刀片:

<input type="text" name="checkout_total" value="{{Cart::subTotal('0') }} ">

Helppp

3 个答案:

答案 0 :(得分:3)

问题是列pay_totals无法存储您从输入中获得的任何内容,因为它太大了。

可能的解决方案

SQL: ALTER TABLE [orders] ALTER COLUMN [pay_totals] VARCHAR(MAX)

MYSQL: ALTER TABLE [orders] MODIFY COLUMN [pay_totals] VARCHAR(60000)

答案 1 :(得分:2)

由于数据类型不匹配,可能会发生相同的错误。例如,如果您要分配一个要浮动的字符串,则会出现此错误。因此请务必检查数据类型是否正确。我在 symfony 上收到此错误。

答案 2 :(得分:0)

有时会由于将数据插入到数据透视表中的('created_at','updated_At')之类的额外列中而发生此错误。下面的解决方案为我工作。

错误代码

$users=User::create($input['data']);
foreach ($input['products'] as $product) {
    if (array_key_exists('special_price', $product)) {
        $userProducts[$product['id']] = ['special_price' => $product['special_price']];
    } else {
        $userProducts[$product['id']] = ['special_price' => ''];
    }
    $userProducts['created_at'] = Carbon::now()->toDateTimeString();
    $userProducts['updated_at'] = Carbon::now()->toDateTimeString();
}

//individual product with special price of each product
/*
Output before code correction

array (1 =>
array(
    'special_price' => '',
),
'created_at' => '2019-04-07 14:28:27',
'updated_at' => '2019-04-07 14:28:27',))
*/
$users->products()->sync($userProducts);

代码更正

$users=User::create($input['data']);
foreach ($input['products'] as $product) {
    if (array_key_exists('special_price', $product)) {
        $userProducts[$product['id']] = ['special_price' => $product['special_price']];
    } else {
        $userProducts[$product['id']] = ['special_price' => ''];
    }
    // below lines are after code correction
    $userProducts[$product['id']]['created_at'] = Carbon::now()->toDateTimeString();
    $userProducts[$product['id']]['updated_at'] = Carbon::now()->toDateTimeString();
}

//individual product with special price of each product
/*
Output after code correction

array (1 =>
array(
    'special_price' => '',
    'created_at' => '2019-04-07 14:28:27',
    'updated_at' => '2019-04-07 14:28:27',
),)
*/
$users->products()->sync($userProducts);