doutun9179 2017-08-06 07:20
浏览 36
已采纳

如何在Laravel中解决此数据更新错误

I want to Update a Registration Form data. So , at first I have created a Table for the show data. In this table , I have included an ViewForUpdate button and I have passed the Id. When I click that ViewForUpdate button , it shows unique data correctly in an another page. After I inputted new data and when I click update button , it shows this error -

MethodNotAllowedHttpException

So , How to fix this ??

Here is the RegViewUpdate.blade.php file

<html>
<head>

<body>

<form action="edit{{$users[0]->id}}" method="post" enctype="multipart/form-data">

{{ method_field('PUT') }}
{{ csrf_field() }}

<div class="form-group">
    <label>Name : *</label>
    <input type="text" class="form-control" name="name" value="{{$users[0]->name}}" required>
  </div>

  <div class="form-group">
    <label>Username : *</label>
    <input type="text" class="form-control" name="username" value="{{$users[0]->username}}" required>
  </div>

  <div class="form-group">
    <label>Password : *</label>
    <input type="password" class="form-control" name="password" value="{{$users[0]->pw}}" required>
  </div>

  <div class="form-group">
    <label>Upload Profile Picture :</label>
    <input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
    <small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
  </div>

  <input type="submit" class="btn btn-primary" name="submit" value="Update">

</form>

</body>
</html>

Here is the RegViewController.php file

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class RegViewController extends Controller
{

    public function index()
    {
        return view('RegView');
    }

    public function show($id) {
        $users = DB::select('select * from academic where id = ?',[$id]);
        return view('RegViewUpdate',['users'=>$users]);
    }

    public function edit(Request $request, $id)
    {
        $name = $request->input('name');

        DB::update('update academic set name = ? where id = ?',[$name,$id]);
        echo "Record updated successfully.<br/>";

    }

}

Here is the Routes that I have created.

Route::get('edit/{id}','RegViewController@show');
Route::post('edit{id}','RegViewController@edit');

academic table Structure.

enter image description here

  • 写回答

4条回答 默认 最新

  • duanniwu7730 2017-08-06 13:12
    关注

    Try this:

    RegViewUpdate.blade.php

    <html>
    <head>
    
    <body>
    
    <form action="edit/{{$users[0]->id}}" method="post" enctype="multipart/form-data">
    
    {{ method_field('PUT') }}
    {{ csrf_field() }}
    
    <div class="form-group">
        <label>Name : *</label>
        <input type="text" class="form-control" name="name" value="{{$users[0]->name}}" required>
      </div>
    
      <div class="form-group">
        <label>Username : *</label>
        <input type="text" class="form-control" name="username" value="{{$users[0]->username}}" required>
      </div>
    
      <div class="form-group">
        <label>Password : *</label>
        <input type="password" class="form-control" name="password" value="{{$users[0]->pw}}" required>
      </div>
    
      <div class="form-group">
        <label>Upload Profile Picture :</label>
        <input type="file" class="form-control-file" name="file_img" aria-describedby="fileHelp">
        <small id="fileHelp" class="form-text text-muted">If U Want , U Can Skip Upload A Profile Picture</small>
      </div>
    
      <input type="submit" class="btn btn-primary" name="submit" value="Update">
    
    </form>
    
    </body>
    </html>
    

    RegViewController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use DB;
    
    class RegViewController extends Controller
    {
    
        public function index()
        {
            return view('RegView');
        }
    
        public function show($id) {
            $users = DB::select('select * from academic where id = ?',[$id]);
            return view('RegViewUpdate',['users'=>$users]);
        }
    
        public function edit(Request $request, $id)
        {
            $name = $request->input('name');
            try {
                 DB::table('academic')
                      ->where('id', $id)
                      ->update(['name' => $name]);
                 echo "Record updated successfully.<br/>";
            }  catch (\Exception $ex) {
                 dd($ex);
            }
        }
    
    }
    

    Routes:

    Route::get('edit/{id}','RegViewController@show');
    Route::put('edit/{id}','RegViewController@edit');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端