Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:不错, 继续努力
//用户添加路由
Route::get('/user/add','UserController@add');
Route::post('/user/store','UserController@store');
Route::get('/user/index','UserController@index');
Route::get('/user/edit/{id}','UserController@edit');
Route::post('/user/save','UserController@save');
Route::get('/user/delete/{id}','UserController@delete');
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function index(){
$data = DB::select('select * from user');
return view('user.list',['data'=>$data]);
}
public function add(){
return view('user.add');
}
public function store(Request $request){
$username = $request->input('username');
$password = $request->input('pwd');
$password = md5($password);
$res = DB::insert('insert into user(`username`,`password`)values(?,?)',[$username,$password]);
if ($res){
echo json_encode(array('code'=>1,'msg'=>'添加成功'));
}else{
echo json_encode(array('code'=>0,'msg'=>'添加失败'));
}
}
public function edit($id){
$data = DB::select('select * from user where id=?',[$id]);
return view('user.edit',['data'=>$data]);
}
public function save(Request $request){
$id = $request->input('id');
$username = $request->input('username');
$password = md5($request->input('pwd'));
$res = DB::update('update user set username=?,password=? where id=?',[$username,$password,$id]);
if ($res){
echo json_encode(array('code'=>1,'msg'=>'编辑成功'));
}else{
echo json_encode(array('code'=>0,'msg'=>'编辑失败'));
}
}
public function delete($id){
$res = DB::delete('delete from user where id=?',[$id]);
if ($res){
echo '<script>alert("删除成功");window.location.href="/user/index";</script>';
}else{
echo '<script>alert("删除失败");window.reload();</script>';
}
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>展示界面</title>
<style>
a{text-decoration: none;}
</style>
</head>
<body>
<button><a href="/user/add">添加</a></button>
<table border="1" cellpadding="4" style="border-collapse: collapse;">
<caption>展示界面</caption>
<tr>
<th>ID</th>
<th>账号</th>
<th>密码</th>
<th>操作</th>
</tr>
@foreach($data as $item)
<tr>
<td>{{$item->id}}</td>
<td>{{$item->username}}</td>
<td>{{$item->password}}</td>
<td><button><a href='/user/edit/{{$item->id}}'>编辑</a></button>
<button><a href='/user/delete/{{$item->id}}' onclick="if(confirm('是否删除数据?')==false)return false;">删除</a></button>
</td>
</tr>
@endforeach
</table>
<script>
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>添加界面</title>
<script src="{{asset('js/jquery.min.js')}}"></script>
</head>
<body>
<form action="" name="add_form">
<table border="1" cellpadding="4" style="border-collapse:collapse;">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<caption>添加用户信息</caption>
<tr>
<td>账号</td>
<td><input type="text" name="username" placeholder="请输入用户名"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd" placeholder="请输入密码"></td>
</tr>
<tr>
<td>确认密码</td>
<td><input type="password" name="repwd" placeholder="请输入确认密码"></td>
</tr>
<tr>
<td colspan="2"><button type="button" onclick="doSave()">保存</button></td>
</tr>
</table>
</form>
<script>
function doSave() {
var username = $.trim($('input[name="username"]').val());
var pwd = $.trim($('input[name="pwd"]').val());
var repwd = $.trim($('input[name="repwd"]').val());
if (username==''){
alert('请输入用户名');
return;
}
if(pwd==''){
alert('请输入密码');
return;
}
if (repwd!=pwd){
alert('确认密码错误');
return;
}
$.post('/user/store',$('form[name="add_form"]').serialize(),function (res) {
if(res.code==0){
alert(res.msg);
return;
}else{
alert(res.msg);
window.location.href = '/user/index';
}
},'json');
}
</script>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>编辑界面</title>
<script src="{{asset('js/jquery.min.js')}}"></script>
</head>
<body>
<form action="" name="add_form">
<table border="1" cellpadding="4" style="border-collapse:collapse;">
<input type="hidden" name="_token" value="{{csrf_token()}}">
@foreach ($data as $item)
<input type="hidden" name="id" value="{{$item->id}}">
<caption>编辑用户信息</caption>
<tr>
<td>账号</td>
<td><input type="text" name="username" placeholder="请输入用户名" value="{{$item->username}}"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="pwd" placeholder="请输入密码"></td>
</tr>
<tr>
<td colspan="2"><button type="button" onclick="doSave()">保存</button></td>
</tr>
@endforeach
</table>
</form>
<script>
function doSave() {
var username = $.trim($('input[name="username"]').val());
var pwd = $.trim($('input[name="pwd"]').val());
if (username==''){
alert('请输入用户名');
return;
}
$.post('/user/save',$('form[name="add_form"]').serialize(),function (res) {
if(res.code==0){
alert(res.msg);
return;
}else{
alert(res.msg);
window.location.href = '/user/index';
}
},'json');
}
</script>
</body>
</html>