博客列表 >12月30日—laravel框架的使用原生SQL对数据库进行增删查改操作

12月30日—laravel框架的使用原生SQL对数据库进行增删查改操作

曾龙宇
曾龙宇原创
2019年12月31日 11:30:35927浏览

路由文件

  1. //用户添加路由
  2. Route::get('/user/add','UserController@add');
  3. Route::post('/user/store','UserController@store');
  4. Route::get('/user/index','UserController@index');
  5. Route::get('/user/edit/{id}','UserController@edit');
  6. Route::post('/user/save','UserController@save');
  7. Route::get('/user/delete/{id}','UserController@delete');

Controller文件

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\User;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. class UserController extends Controller
  7. {
  8. public function index(){
  9. $data = DB::select('select * from user');
  10. return view('user.list',['data'=>$data]);
  11. }
  12. public function add(){
  13. return view('user.add');
  14. }
  15. public function store(Request $request){
  16. $username = $request->input('username');
  17. $password = $request->input('pwd');
  18. $password = md5($password);
  19. $res = DB::insert('insert into user(`username`,`password`)values(?,?)',[$username,$password]);
  20. if ($res){
  21. echo json_encode(array('code'=>1,'msg'=>'添加成功'));
  22. }else{
  23. echo json_encode(array('code'=>0,'msg'=>'添加失败'));
  24. }
  25. }
  26. public function edit($id){
  27. $data = DB::select('select * from user where id=?',[$id]);
  28. return view('user.edit',['data'=>$data]);
  29. }
  30. public function save(Request $request){
  31. $id = $request->input('id');
  32. $username = $request->input('username');
  33. $password = md5($request->input('pwd'));
  34. $res = DB::update('update user set username=?,password=? where id=?',[$username,$password,$id]);
  35. if ($res){
  36. echo json_encode(array('code'=>1,'msg'=>'编辑成功'));
  37. }else{
  38. echo json_encode(array('code'=>0,'msg'=>'编辑失败'));
  39. }
  40. }
  41. public function delete($id){
  42. $res = DB::delete('delete from user where id=?',[$id]);
  43. if ($res){
  44. echo '<script>alert("删除成功");window.location.href="/user/index";</script>';
  45. }else{
  46. echo '<script>alert("删除失败");window.reload();</script>';
  47. }
  48. }
  49. }

list界面

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>展示界面</title>
  9. <style>
  10. a{text-decoration: none;}
  11. </style>
  12. </head>
  13. <body>
  14. <button><a href="/user/add">添加</a></button>
  15. <table border="1" cellpadding="4" style="border-collapse: collapse;">
  16. <caption>展示界面</caption>
  17. <tr>
  18. <th>ID</th>
  19. <th>账号</th>
  20. <th>密码</th>
  21. <th>操作</th>
  22. </tr>
  23. @foreach($data as $item)
  24. <tr>
  25. <td>{{$item->id}}</td>
  26. <td>{{$item->username}}</td>
  27. <td>{{$item->password}}</td>
  28. <td><button><a href='/user/edit/{{$item->id}}'>编辑</a></button>&nbsp;
  29. <button><a href='/user/delete/{{$item->id}}' onclick="if(confirm('是否删除数据?')==false)return false;">删除</a></button>
  30. </td>
  31. </tr>
  32. @endforeach
  33. </table>
  34. <script>
  35. </script>
  36. </body>
  37. </html>

add界面

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>添加界面</title>
  9. <script src="{{asset('js/jquery.min.js')}}"></script>
  10. </head>
  11. <body>
  12. <form action="" name="add_form">
  13. <table border="1" cellpadding="4" style="border-collapse:collapse;">
  14. <input type="hidden" name="_token" value="{{csrf_token()}}">
  15. <caption>添加用户信息</caption>
  16. <tr>
  17. <td>账号</td>
  18. <td><input type="text" name="username" placeholder="请输入用户名"></td>
  19. </tr>
  20. <tr>
  21. <td>密码</td>
  22. <td><input type="password" name="pwd" placeholder="请输入密码"></td>
  23. </tr>
  24. <tr>
  25. <td>确认密码</td>
  26. <td><input type="password" name="repwd" placeholder="请输入确认密码"></td>
  27. </tr>
  28. <tr>
  29. <td colspan="2"><button type="button" onclick="doSave()">保存</button></td>
  30. </tr>
  31. </table>
  32. </form>
  33. <script>
  34. function doSave() {
  35. var username = $.trim($('input[name="username"]').val());
  36. var pwd = $.trim($('input[name="pwd"]').val());
  37. var repwd = $.trim($('input[name="repwd"]').val());
  38. if (username==''){
  39. alert('请输入用户名');
  40. return;
  41. }
  42. if(pwd==''){
  43. alert('请输入密码');
  44. return;
  45. }
  46. if (repwd!=pwd){
  47. alert('确认密码错误');
  48. return;
  49. }
  50. $.post('/user/store',$('form[name="add_form"]').serialize(),function (res) {
  51. if(res.code==0){
  52. alert(res.msg);
  53. return;
  54. }else{
  55. alert(res.msg);
  56. window.location.href = '/user/index';
  57. }
  58. },'json');
  59. }
  60. </script>
  61. </body>
  62. </html>

edit界面

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>编辑界面</title>
  9. <script src="{{asset('js/jquery.min.js')}}"></script>
  10. </head>
  11. <body>
  12. <form action="" name="add_form">
  13. <table border="1" cellpadding="4" style="border-collapse:collapse;">
  14. <input type="hidden" name="_token" value="{{csrf_token()}}">
  15. @foreach ($data as $item)
  16. <input type="hidden" name="id" value="{{$item->id}}">
  17. <caption>编辑用户信息</caption>
  18. <tr>
  19. <td>账号</td>
  20. <td><input type="text" name="username" placeholder="请输入用户名" value="{{$item->username}}"></td>
  21. </tr>
  22. <tr>
  23. <td>密码</td>
  24. <td><input type="password" name="pwd" placeholder="请输入密码"></td>
  25. </tr>
  26. <tr>
  27. <td colspan="2"><button type="button" onclick="doSave()">保存</button></td>
  28. </tr>
  29. @endforeach
  30. </table>
  31. </form>
  32. <script>
  33. function doSave() {
  34. var username = $.trim($('input[name="username"]').val());
  35. var pwd = $.trim($('input[name="pwd"]').val());
  36. if (username==''){
  37. alert('请输入用户名');
  38. return;
  39. }
  40. $.post('/user/save',$('form[name="add_form"]').serialize(),function (res) {
  41. if(res.code==0){
  42. alert(res.msg);
  43. return;
  44. }else{
  45. alert(res.msg);
  46. window.location.href = '/user/index';
  47. }
  48. },'json');
  49. }
  50. </script>
  51. </body>
  52. </html>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议