代码之家  ›  专栏  ›  技术社区  ›  The Dead Man

Laravel:SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;

  •  0
  • The Dead Man  · 技术社区  · 7 年前

    我有一个简单的crud应用程序,只有一个输入, post title

    这是邮政桌

    邮政桌

    id  title
    1   sdss
    2   pani
    

    这是我在post controller中的更新功能

    public function update(Request $request, $id)
    {
        $post = Post::find($id);
        $post->$request = $request->get('title');
        $post->save();
    
        return redirect("/posts")->with("success", "Data updated");
    }
    

    class Post extends Model
    {
        protected $fillable =["title"];
    }
    

    这里是html

    @section('content')
      <div class="content">
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
    
              <form method="post" action="{{ route('posts.update', $post->id) }}" class="form-horizontal">
                @csrf
                @method('PATCH') 
    
                <div class="card ">
                  <div class="card-header card-header-primary">
                    <h4 class="card-title">{{ __('Edit Page Details') }}</h4>
                    <p class="card-category"></p>
                  </div>
                  <div class="card-body ">
                    <div class="row">
                      <div class="col-md-12 text-right">
                          <a href="{{ route('posts.index')}}" class="btn btn-sm btn-primary">{{ __('Back to list') }}</a>
                      </div>
                    </div>
                    <div class="row">
                      <label class="col-sm-2 col-form-label">{{ __(' Title') }}</label>
                      <div class="col-sm-7">
                            <div class="form-group">    
    
                            <input type="text" class="form-control" name="title" value="{{$post->title}}"/>
                            </div>
                      </div>
                    </div>
    
                  </div>
                  <div class="card-footer ml-auto mr-auto">
                    <button type="submit" class="btn btn-primary">{{ __('Save') }}</button>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    @endsection
    

    现在,当我点击save按钮更新数据时,我得到了以下错误

    SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the 
    manual that corresponds to your MariaDB server version for the right syntax to use near '.`8,
    application/signed-exchange;v=b3 Accept-Encoding: gzip, deflate, 
    ' at line 1 (SQL: update `posts` set `PATCH /posts/2 HTTP/1`.`1 Accept:
    

    我需要做什么来解决这个问题?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Salim Djerbouh SeliC    7 年前

    如果你想更新标题,不应该是这样吗?

    $post->title = $request->get('title');
    

    而不是

    $post->$request = $request->get('title');
    

    post模型没有名为的属性 $request

    $fillable 属性是不相关的,因为您没有执行体量指定