ThinkPhp5 save方法保存数据demo实例
路由器
Route::resource('article', 'article/index');
添加跳转链接
<a href="create">添加</a>
控制器中create方法
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
return $this->fetch('create');
}
create.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文章添加</title>
</head>
<body>
<h3>文章添加</h3>
<form action="save" method="post" enctype="multipart/form-data">
文章标题:<input type="text" name="title" value=""><br/><br/>
文章描述:<input type="text" name="description" value=""><br/><br/>
<button type="submit">提交</button>
</form>
</body>
</html>
控制器中save方法
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save()
{
$row = DB::name('article')->insert($_POST);
if($row){
return "<script>alert('添加成功');window.location.href='index';</script>";
}
}
注:
使用DB::name的前提是已经在config/database.php中配置了数据表前缀
'prefix' => '',