简体   繁体   中英

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list' (SQL: insert into `products`

This Code I got from the internet. I copy and paste it Run. when I try to insert product, this error comes.

How to fix it....................................................................................................

In Chrome,

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list' (SQL: insert into `products` (`_token`, `title`, `product_code`, `description`, `image`) values (c7cye8TIyVIIQytZrY5fKjPmGzytoMsKPHNMY6US, efdfs, 243423, sdfsadfsd, C:\xampp\tmp\phpA0FA.tmp))

In Controller,

public function store(Request $request)
{
$request->validate([
'title' => 'required',
'product_code' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'description' => 'required',
]);
if ($files = $request->file('image')) {
$destinationPath = 'public/image/'; // upload path
$profileImage = date('YmdHis') . "." . $files->getClientOriginalExtension();
$files->move($destinationPath, $profileImage);
$insert['image'] = "$profileImage";
}
$insert['title'] = $request->get('title');
$insert['product_code'] = $request->get('product_code');
$insert['description'] = $request->get('description');
Product::insert($request->all());
return Redirect::to('products')
->with('success','Greate! Product created successfully.');
}

In Create page,

<form action="{{ route('products.store') }}" method="POST" name="add_product" enctype="multipart/form-data">
    {{ csrf_field() }}
    <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <strong>Title</strong>
                <input type="text" name="title" class="form-control" placeholder="Enter Title">
                <span class="text-danger">{{ $errors->first('title') }}</span>
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <strong>Product Code</strong>
                <input type="text" name="product_code" class="form-control" placeholder="Enter Product Code">
                <span class="text-danger">{{ $errors->first('product_code') }}</span>
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <strong>Description</strong>
                <textarea class="form-control" col="4" name="description" placeholder="Enter Description"></textarea>
                <span class="text-danger">{{ $errors->first('description') }}</span>
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <strong>Product Image</strong>
                <input type="file" name="image" class="form-control" placeholder="">
                <span class="text-danger">{{ $errors->first('image') }}</span>
            </div>
        </div>
        <div class="col-md-12">
            <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
</form>

您最好使用$request->only ('specify here fields to insert') 而不是$request->all() - 提交的表单中有 csrf 令牌

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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