简体   繁体   中英

Laravel Database Update using Form

Im trying to update my sqlite database using a form. I first retrieve the contents of the table row and display it in a form, i can then edit the contents of the form and when im done press update, which should update the table and redirect to 'home' but instead i receive this error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

for what reason am i receiving this message?

Heres the code thats executing:

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));
}

Routes:

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;
}

The form that executes the route "update":

@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

I think you can't run the function updateStatus like this..

First create an controller StatusController. Then you do it like this:

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');
}

Routes

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

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