语法 | 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