Laravel 无法在数据库中更新 __ 创建正在工作

发布于 2025-01-13 14:02:01 字数 5270 浏览 1 评论 0原文

我遇到的问题是我单击单独页面上的显示,然后编辑数据。 但是当我点击更新时什么也没有发生。

控制器

 public function edit(Request $request,$id)
{
    //
    // $clients = Client::find($client);
    // return view('clients.edit',compact('clients'));
    // ->with('clients', $clients)


    $id = $request->input('id');
    $ClientName = $request->input('ClientName');
    $ClientLastname = $request->input('ClientLastname');
    $ClientCin = $request->input('ClientCin');
    $ClientPhone = $request->input('ClientPhone');

    DB::update('update clients set ClientName = ?,ClientLastname=?,ClientCin=?,ClientPhone=? where id = ?',[$ClientName,$ClientLastname,$ClientCin,$ClientPhone,$id]);

    echo "Record updated successfully.<br/>";
   // echo '<a href = "">Click Here</a> to go back.';
}
 public function update($id)
{


    $clients = DB::select('select * from clients where id = ?',[$id]);
    return view('clients.edit',['clients'=>$clients]);
}

编辑页面:

   <form action="{{ route('clients.edit.post',$clients[0]->id) }}" method="POST">

                @csrf
                {{-- @method('PUT') --}}

                <div class="row">
                    <div class="form-floating mb-3">
                        <input
                        type="text" class="form-control" placeholder="ID"  id="floatingInput"
                        name="id"
                        value="{{ $clients[0]->id }}"
                        disabled>
                        <label for="floatingInput">Votre ID</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input
                        type="text" class="form-control" placeholder="Votre Prenom"
                        id="floatingInput"
                        name="ClientName"
                        value="{{ $clients[0]->ClientName }}">
                        <label for="floatingInput">Votre Prenom</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control" placeholder="Votre Nom"
                        id="floatingInput"
                        name="ClientLastname"
                        value="{{ $clients[0]->ClientLastname }}">
                        <label for="floatingInput">Votre Nom</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control" placeholder="Votre Cin"
                        id="floatingInput"
                        name="ClientCin"
                        value="{{ $clients[0]->ClientCin }}">
                        <label for="floatingInput">Votre Cin</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control" placeholder="Votre Telephone"
                        id="floatingInput"
                        name="ClientPhone"
                        value="{{ $clients[0]->ClientPhone }}">
                        <label for="floatingInput">Votre Telephone</label>
                    </div>
                </div>
                <div class="d-flex align-items-center justify-content-center mb-4">
                    <button class="btn rounded-pill btn-success ms-2" type="submit">
                        Modifier
                    </button>
                </div>
            </form>
        </div>

表格上的字段:

 <form action="" method="POST">
                                    <a href="{{ route('clients.show', $item->id) }}"
                                        class="btn btn-sm rounded-pill mb-2 btn-info">
                                        Afficher
                                    </a>

                                    <a href='clients/edit/{{ $item->id }}'
                                        class="btn btn-sm rounded-pill btn-warning mb-2">
                                        Modifier
                                    </a>
                                    {{ method_field('DELETE') }}
                                    {{ csrf_field() }}
                                    <button type="submit" class="btn btn-sm rounded-pill btn-danger mb-2"
                                        onclick="return confirm(&quot;Confirm delete?&quot;)">
                                        Supprimer
                                    </button>
                                </form>

<强>路线:

Route::get('receptions/clients/edit',[ClientController::class ,'edit'])->name('clients.edit');
Route::get('receptions/clients/edit/{id}',[ClientController::class ,'update'])->name('clients.update');
Route::post('receptions/clients/edit/{id}',[ClientController::class ,'edit'])->name('clients.edit.post');

The problem i have is i click on show on a seperate page then i edit the data.
but when i click on update nothing happen.

Controller

 public function edit(Request $request,$id)
{
    //
    // $clients = Client::find($client);
    // return view('clients.edit',compact('clients'));
    // ->with('clients', $clients)


    $id = $request->input('id');
    $ClientName = $request->input('ClientName');
    $ClientLastname = $request->input('ClientLastname');
    $ClientCin = $request->input('ClientCin');
    $ClientPhone = $request->input('ClientPhone');

    DB::update('update clients set ClientName = ?,ClientLastname=?,ClientCin=?,ClientPhone=? where id = ?',[$ClientName,$ClientLastname,$ClientCin,$ClientPhone,$id]);

    echo "Record updated successfully.<br/>";
   // echo '<a href = "">Click Here</a> to go back.';
}
 public function update($id)
{


    $clients = DB::select('select * from clients where id = ?',[$id]);
    return view('clients.edit',['clients'=>$clients]);
}

the edit page :

   <form action="{{ route('clients.edit.post',$clients[0]->id) }}" method="POST">

                @csrf
                {{-- @method('PUT') --}}

                <div class="row">
                    <div class="form-floating mb-3">
                        <input
                        type="text" class="form-control" placeholder="ID"  id="floatingInput"
                        name="id"
                        value="{{ $clients[0]->id }}"
                        disabled>
                        <label for="floatingInput">Votre ID</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input
                        type="text" class="form-control" placeholder="Votre Prenom"
                        id="floatingInput"
                        name="ClientName"
                        value="{{ $clients[0]->ClientName }}">
                        <label for="floatingInput">Votre Prenom</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control" placeholder="Votre Nom"
                        id="floatingInput"
                        name="ClientLastname"
                        value="{{ $clients[0]->ClientLastname }}">
                        <label for="floatingInput">Votre Nom</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control" placeholder="Votre Cin"
                        id="floatingInput"
                        name="ClientCin"
                        value="{{ $clients[0]->ClientCin }}">
                        <label for="floatingInput">Votre Cin</label>
                    </div>
                    <div class="form-floating mb-3">
                        <input type="text" class="form-control" placeholder="Votre Telephone"
                        id="floatingInput"
                        name="ClientPhone"
                        value="{{ $clients[0]->ClientPhone }}">
                        <label for="floatingInput">Votre Telephone</label>
                    </div>
                </div>
                <div class="d-flex align-items-center justify-content-center mb-4">
                    <button class="btn rounded-pill btn-success ms-2" type="submit">
                        Modifier
                    </button>
                </div>
            </form>
        </div>

the fields on the table:

 <form action="" method="POST">
                                    <a href="{{ route('clients.show', $item->id) }}"
                                        class="btn btn-sm rounded-pill mb-2 btn-info">
                                        Afficher
                                    </a>

                                    <a href='clients/edit/{{ $item->id }}'
                                        class="btn btn-sm rounded-pill btn-warning mb-2">
                                        Modifier
                                    </a>
                                    {{ method_field('DELETE') }}
                                    {{ csrf_field() }}
                                    <button type="submit" class="btn btn-sm rounded-pill btn-danger mb-2"
                                        onclick="return confirm("Confirm delete?")">
                                        Supprimer
                                    </button>
                                </form>

The routes :

Route::get('receptions/clients/edit',[ClientController::class ,'edit'])->name('clients.edit');
Route::get('receptions/clients/edit/{id}',[ClientController::class ,'update'])->name('clients.update');
Route::post('receptions/clients/edit/{id}',[ClientController::class ,'edit'])->name('clients.edit.post');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2025-01-20 14:02:01

更好的方法:

DB::table('clients')
    ->where('id', $id)
    ->update([
      'ClientName' => $ClientName,
      'ClientLastname' => $ClientLastname,
      'ClientCin' => $ClientCin,
      'ClientPhone' => $ClientPhone
    ]);

https://laravel.com/docs/9.x/queries #更新语句

Better way:

DB::table('clients')
    ->where('id', $id)
    ->update([
      'ClientName' => $ClientName,
      'ClientLastname' => $ClientLastname,
      'ClientCin' => $ClientCin,
      'ClientPhone' => $ClientPhone
    ]);

https://laravel.com/docs/9.x/queries#update-statements

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文