1. 저장 프로시저 (insertAdmin) 생성

$ mysql -u xyz -pxyz123 xyz
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
DELIMITER $$
CREATE PROCEDURE insertAdmin
  (userid VARCHAR(255),
   password VARCHAR(255),
   nick VARCHAR(255))
BEGIN
  INSERT INTO admins(userid, password, nick) VALUES(userid, password, nick);
END $$
DELIMITER ;
mysql> SELECT * FROM admins;
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
+-----+---------+----------+-------+
5 ROWS IN SET (0.00 sec)
 
mysql> CALL insertAdmin('testid6', 'passwd6', 'nick6');
Query OK, 1 ROW affected (0.01 sec)
 
mysql> SELECT * FROM admins;
+-----+---------+----------+-------+
| sno | userid  | password | nick  |
+-----+---------+----------+-------+
|   1 | testid1 | passwd1  | nick1 |
|   2 | testid2 | passwd2  | nick2 |
|   3 | testid3 | passwd3  | nick3 |
|   4 | testid4 | passwd4  | nick4 |
|   5 | testid5 | passwd5  | nick5 |
|   6 | testid6 | passwd6  | nick6 |
+-----+---------+----------+-------+
6 ROWS IN SET (0.00 sec)
 
mysql> exit

 

2. xyz/resources/views/mgmt/admin/index.blade.php 에 다음을 추가한다.

...
 
<div style="text-align: right; padding-bottom: 10px">
  <a href="/mgmt/admin/insert_form" class="btn btn-default" data-toggle="modal" data-target="#myModal">관리자 추가</a>
</div>
 
...
 
<div id="myModal" class="modal fade" role="dialog" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
    </div>
  </div>
</div>
 
...
 
<script>
// Modal Remote Reload
$(document).on('hidden.bs.modal', function (e) {
    $(e.target).removeData('bs.modal');
})
</script>
 
...

 

3. http://xyz.test.com/mgmt/admin 을 다시 보면, 관리자 추가 버튼이 새로이 나타날 것이다.

 

4. 관리자 입력 양식을 만들자. xyz/resources/views/mgmt/admin/insert_form.blade.php

<div class="modal-header">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  <h4 class="modal-title">관리자 추가</h4>
</div>
<div class="modal-body">
  <form name="insert_form" action="/mgmt/admin/insert" method="post">
    @csrf
    <div class="form-group">
      <label>아이디 <small>(필수)</small></label>
      <input type="text" name="userid" class="form-control" required>
    </div>
    <div class="form-group">
      <label>비밀번호 <small>(필수)</small></label>
      <input type="password" id="password" name="passwd1" class="form-control" required>
    </div>
    <div class="form-group">
      <label>비밀번호 확인 <small>(필수)</small></label>
      <input type="password" name="passwd2" class="form-control" required>
    </div>
    <div class="form-group">
      <label>별명 <small>(필수)</small></label>
      <input type="text" name="nick" class="form-control" required>
    </div>
    <div class="form-group" style="text-align: right">
      <input class="btn btn-primary" type="submit" value="관리자 추가" />
    </div>
  </form>
</div>

 

5. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음 코드를 추가한다.

...
 
    // 관리자 추가 폼
    public function insert_form () {
        return view('mgmt.admin.insert_form');
    }
 
...

 

6. xyz/routes/web.php 에서 mgmt 그룹안에 다음을 추가한다.

    Route::get('admin/insert_form', [Admin::class, 'insert_form']);

 

7. 관리자 추가 버튼을 클릭하면 모달 다이얼로그 박스 형식의 입력 양식이 나올 것이다.

 

8. 이제, 실제로 DB에 관리자를 추가해보자. 이제부터는 Model 클래스를 작성하지 않는다. Controller 에서 직접 DB에 접근해서 Stored Procedure 를 호출할 것이다. xyz/app/Http/Controllers/Mgmt/Admin.php 에 다음을 추가한다.

...
 
use Illuminate\Http\Request;
 
...
 
    // 관리자 추가
    public function insert (Request $request) {
        $userid  = $request->input('userid');
        $passwd1 = $request->input('passwd1');
        $passwd2 = $request->input('passwd2');
        $nick    = $request->input('nick');
 
        if ($passwd1 != $passwd2) {
            return redirect('/mgmt/admin');
        }
        // $model = new AdminModel();
        // $model->insertAdmin($userid, $passwd1, $nick);
        DB::insert('CALL insertAdmin(?, ?, ?)', [$userid, $passwd1, $nick]);
 
        return redirect('/mgmt/admin');
    }
 
...

 

9. xyz/routes/web.php 에서 mgmt 그룹안에 다음을 추가한다.

    Route::post('admin/insert', [Admin::class, 'insert']);

 

10. 코드 작성은 완료되었으니, 실제 화면에서 관리자를 등록해보면 목록에 표시되는 것을 볼 수 있을 것이다.

 

 

+ Recent posts