Laravel 无法在数据库中更新 __ 创建正在工作
我遇到的问题是我单击单独页面上的显示,然后编辑数据。 但是当我点击更新时什么也没有发生。
控制器
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("Confirm delete?")">
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的方法:
https://laravel.com/docs/9.x/queries #更新语句
Better way:
https://laravel.com/docs/9.x/queries#update-statements