語法 | 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','[email protected]'); Route::post('create','[email protected]');
http://localhost:8000/insert