使用表单的Laravel数据库更新

时间:2015-04-16 06:46:02

标签: php sqlite laravel

我试图使用表单更新我的sqlite数据库。我首先检索表格行的内容并以表格形式显示,然后我可以编辑表格的内容,当我完成后按更新,这应该更新表格并重定向到主页'但我得到了这个错误:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

我收到此消息的原因是什么?

继承执行的代码:

function updateStatus(){
    $id = Input::get('id');
    $uptitle = Input::get('title');
    $upmessage = Input::get('message');


    $sql = "UPDATE status SET title= ? Message= ? WHERE Id= ?";
    DB::update($sql, array($uptitle, $upmessage, $id));
}

路线:

Route::post('updated',function()
{
    updateStatus();

    $results = getPosts();

    return Redirect::to('home')->withPosts($results);
});



function getPosts()
{
    $sql = "select * from status order by Id DESC";
    $results = DB::select($sql);
    return $results;
}

执行路线的表格"更新":

@section('content')
@forelse ($edits as $edit)
<form method="post" action="updated" >
      <div class="form-group">
          <input type="hidden" name="id" value="{{{ $edit->Id }}}"> 
         <!-- <label>Name</label>
                <input name ="name" class="form-control" value='{{{ $edit->Name }}}'>
          -->
            <label>Post Title</label>
                <input name ="title" class="form-control"  value='{{{ $edit->Title }}}'>
      </div>
      <div class="form-group">
            <label>Message</label>
            <textarea class="form-control" name="message"  rows="3" >{{{ $edit->Message }}}</textarea>
      </div>


    <button type="update" class="btn btn-default">Update</button>
</form>
@empty
     No Posts
    @endforelse
@stop

1 个答案:

答案 0 :(得分:2)

我认为你不能像这样运行updateStatus函数..

首先创建一个控制器StatusController。然后你这样做:

public function index() {
   $sql = "select * from status order by Id DESC";
   $results = DB::select($sql);
   return $results;
}

public function edit($id) {
   // Get the status
   $edit = Status::find($id);
   // Here return your edit page for updating the status
   return View::make('status.edit', array('edit' => $edit));
}

public function updateStatus($id) {
   // Get the status
   $status = Status::find($id);

   $uptitle = Input::get('title');
   $upmessage = Input::get('message');

   $sql = "UPDATE status SET title= ? Message= ? WHERE Id= ?";
   DB::update($sql, array($uptitle, $upmessage, $status->id));

   return Redirect::to('home');
}

<强>路线

Route::get('statuses', 'StatusController@index');
Route::get('statuses/edit/{id}', 'StatusController@edit');
Route::post('statuses/edit/{id}', 'StatusController@updateStatus');