语法 | bool insert(string $query, array $bindings = array()) |
---|---|
参数 |
|
返回值 | bool |
描述 |
在数据库上运行 INSERT 语句
|
php artisan make:controller StudInsertController
第3步 - 将以下代码复制到文件 - app/Http/Controllers/StudInsertController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; use App\Http\Requests; use App\Http\Controllers\Controller; class StudInsertController extends Controller { public function insertform(){ return view('stud_create'); } public function insert(Request $request){ $name = $request->input('stud_name'); DB::insert('insert into student (name,age) values(?,?)',[$name, $age]); echo "Record inserted successfully.<br/>"; echo '<a href = "/insert">Click Here</a> to go back.'; } }
第4步 - 创建一个名为 resources/views/stud_create.php 的视图文件,并复制下面的代码到此文件中。
resources/views/stud_create.php
<html> <head> <title>添加 - 学生管理</title> </head> <body> <form action = "/create" method = "post"> <input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>"> <table> <tr> <td>名字:</td> <td><input type='text' name='stud_name' /></td> </tr> <tr> <td>年龄:</td> <td><input type='text' name='stud_age' /></td> </tr> <tr> <td colspan = '2'> <input type = 'submit' value = "添加学生"/> </td> </tr> </table> </form> </body> </html>
app/Http/routes.php
Route::get('insert','StudInsertController@insertform'); Route::post('create','StudInsertController@insert');
http://localhost:8000/insert