语法 | int update(string $query, array $bindings = array()) |
---|---|
参数 |
|
返回值 | int |
描述 |
在数据库上运行一个更新语句
|
php artisan make:controller studupdatecontroller
第3步 - 将以下代码复制到文件 - app/http/controllers/studupdatecontroller.php
<?php namespace app\http\controllers; use illuminate\http\request; use db; use app\http\requests; use app\http\controllers\controller; class studupdatecontroller extends controller { public function index(){ $users = db::select('select * from student'); return view('stud_edit_view',['users'=>$users]); } public function show($id) { $users = db::select('select * from student where id = ?',[$id]); return view('stud_update',['users'=>$users]); } public function edit(request $request,$id) { $name = $request->input('stud_name'); db::update('update student set name = ? where id = ?',[$name,$id]); echo "更新记录成功.<br/>"; echo '<a href = "/edit-records">点击这里</a> 返回'; } }
resources/views/stud_edit_view.blade.php
<html> <head> <title>查看学生记录</title> </head> <body> <table border = "1"> <tr> <td>id</td> <td>name</td> <td>edit</td> </tr> @foreach ($users as $user) <tr> <td>{{ $user->id }}</td> <td>{{ $user->name }}</td> <td><a href = 'edit/{{ $user->id }}'>编辑</a></td> </tr> @endforeach </table> </body> </html>
resources/views/stud_update.php
<html> <head> <title>编辑 | 学生管理</title> </head> <body> <form action = "/edit/<?php echo $users[0]->id; ?>" method = "post"> <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>"> <table> <tr> <td>名字</td> <td> <input type = 'text' name = 'stud_name' value = '<?php echo$users[0]->name; ?>'/> </td> </tr> <tr> <td colspan = '2'> <input type = 'submit' value = "更新学生信息" /> </td> </tr> </table> </form> </body> </html>
app/http/routes.php
route::get('edit-records','studupdatecontroller@index'); route::get('edit/{id}','studupdatecontroller@show'); route::post('edit/{id}','studupdatecontroller@edit');
http://localhost:8000/edit-records