안효원 안효원 2022-01-27
CRUD 80%
@23a32d048697778fb84a88ab25a0df975480a22a
 
20220126/Address.php (added)
+++ 20220126/Address.php
@@ -0,0 +1,60 @@
+<?php
+    require_once("DB.php");
+
+
+class Address
+{
+    private $db;
+
+    // address 인스턴스를 생성하면 자동으로 DB 연결
+    public function __construct() {
+        $db = new DB();
+        $this->db = $db->getDB();
+    }
+
+    public function getList() {
+        try {
+            $stmt = $this->db->prepare("select * from address");
+            $stmt->execute();
+            $result = $stmt->fetchAll();
+            return $result;
+        }catch (PDOException $e) {
+            return $e;
+        }
+    }
+
+    public function insertTest($name, $age, $tel, $area, $birth) {
+        try {
+            $stmt = $this->db->prepare("insert into address values(NULL, ?, ?, ?, ?, ?)");
+            $stmt->execute(array($name, $age, $tel, $area, $birth));
+            return true;
+        }catch (PDOException $e) {
+            echo $e;
+            return false;
+        }
+    }
+
+    public function updateTest($no, $name, $age, $tel, $area, $birth) {
+        try {
+            $stmt = $this->db->prepare("update address set name=?, age=?, tel=?, area=?, birth=? where no=?");
+            $stmt->execute(array($no));
+            return true;
+        }catch (PDOException $e) {
+            echo $e;
+            return false;
+        }
+    }
+
+    public function deleteTest($no) {
+        try {
+            $stmt = $this->db->prepare("delete from address where no=?");
+            $stmt->execute(array($no));
+            return true;
+        }catch (PDOException $e){
+            echo $e;
+            return false;
+        }
+    }
+
+
+}(No newline at end of file)
20220126/DB.php
--- 20220126/DB.php
+++ 20220126/DB.php
@@ -43,10 +43,10 @@
         }
     }
 
-    public function updateTest($name) {
+    public function updateTest($name, $where) {
         try {
-            $stmt = $this->db->prepare("update address set name=? where name='안효원'");
-            $stmt->execute(array($name));
+            $stmt = $this->db->prepare("update address set name=? where name=?");
+            $stmt->execute(array($name, $where));
             return true;
         }catch (PDOException $e) {
             echo $e;
 
20220126/address_delete.php (added)
+++ 20220126/address_delete.php
@@ -0,0 +1,16 @@
+<?php
+    require_once("Address.php");
+
+    $no = $_GET['no'];
+
+    $address = new Address();
+
+    $result = $address->deleteTest($no);
+    if($result) {
+        echo "<script>alert('Delete Success!!!')</script>";
+        echo "<script>document.location.href='address_view.php'</script>";
+    }else {
+        echo "<script>alert('Delete Fail!!!')</script>";
+        echo "<script>document.location.href='address_view.php'</script>";
+    }
+
 
20220126/address_insert.php (added)
+++ 20220126/address_insert.php
@@ -0,0 +1,23 @@
+<?php
+    require_once("Address.php");
+
+    $name = $_POST['name'];
+    $age = $_POST['age'];
+    $tel = $_POST['tel'];
+    $area = $_POST['area'];
+    $birth = $_POST['birth'];
+
+    $address = new Address();
+    $result = $address->insertTest($name, $age, $tel, $area, $birth);
+
+    if($result) {
+        echo "<script>alert('Insert Success!!')</script>";
+        echo "<script>document.location.href='address_view.php'</script>";
+    }else {
+        echo "<script>alert('Insert Fail!!')</script>";
+        echo "<script>document.location.href='address_view.php'</script>";
+    }
+
+
+
+
 
20220126/address_update_back.php (added)
+++ 20220126/address_update_back.php
@@ -0,0 +1,19 @@
+<?php
+    require_once("Address.php");
+    $address = new Address();
+
+    $no = $_GET['no'];
+    $name = $_POST['name'];
+    $age = $_POST['age'];
+    $tel = $_POST['tel'];
+    $area = $_POST['area'];
+    $birth = $_POST['birth'];
+
+    $result = $address->updateTest($no, $name, $age, $tel, $area, $birth);
+    if($result) {
+        echo "<script>alert('Update Success!!')</script>";
+        echo "<script>document.location.href='address_view.php'</script>";
+    }else {
+        echo "<script>alert('Update Fail!!')</script>";
+        echo "<script>document.location.href='address_update_front.php?no=$no'</script>";
+    }
 
20220126/address_update_front.php (added)
+++ 20220126/address_update_front.php
@@ -0,0 +1,38 @@
+<!doctype html>
+<html lang="ko">
+<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>Document</title>
+</head>
+<body>
+    <form action="address_update_back.php?no=<?php echo $_GET['no'] ?>" method="post">
+        <label for="name">
+            이름 : <input type="text" name="name" size="5">
+        </label>
+        <label for="age">
+            나이 : <input type="text" name="age" size="2">
+        </label>
+        <label for="tel">
+            연락처 : <input type="text" name="tel" size="8">
+        </label><br>
+        <label for="area">
+            거주지 :
+            <select name="area">
+                <option value="광주">광주</option>
+                <option value="서울">서울</option>
+                <option value="대전">대전</option>
+                <option value="전주">전주</option>
+                <option value="부산">부산</option>
+                <option value="울산">울산</option>
+            </select>
+        </label>
+        <label for="birth">
+            생년월일 : <input type="date" name="birth">
+        </label>
+        <button type="submit">수정</button>
+    </form>
+</body>
+</html>(No newline at end of file)
 
20220126/address_view.php (added)
+++ 20220126/address_view.php
@@ -0,0 +1,84 @@
+<?php
+    require_once("Address.php");
+    $address = new Address();
+    $list = $address->getList();
+
+    $count = 0;
+?>
+<!doctype html>
+<html lang="ko">
+<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>
+        * {text-align: center; margin: 0 auto;}
+        #page {background-color: antiquewhite;}
+        th, td {padding: 10px;}
+    </style>
+</head>
+<body>
+    <div id="page">
+        <h1>주소록 프로그램</h1>
+        <div>
+            <form action="address_insert.php" method="post">
+                <label for="name">
+                    이름 : <input type="text" name="name" size="5">
+                </label>
+                <label for="age">
+                    나이 : <input type="text" name="age" size="2">
+                </label>
+                <label for="tel">
+                    연락처 : <input type="text" name="tel" size="8">
+                </label><br>
+                <label for="area">
+                    거주지 :
+                    <select name="area">
+                        <option value="광주">광주</option>
+                        <option value="서울">서울</option>
+                        <option value="대전">대전</option>
+                        <option value="전주">전주</option>
+                        <option value="부산">부산</option>
+                        <option value="울산">울산</option>
+                    </select>
+                </label>
+                <label for="birth">
+                    생년월일 : <input type="date" name="birth">
+                </label>
+                <button type="submit">추가</button>
+            </form>
+        </div>
+        <hr><br>
+        <table border="1">
+            <tr>
+                <th>번호</th>
+                <th>이름</th>
+                <th>나이</th>
+                <th>연락처</th>
+                <th>거주지</th>
+                <th>생년월일</th>
+                <th>수정</th>
+                <th>삭제</th>
+            </tr>
+            <?php foreach ($list as $value) {
+//                $count = $count + 1;
+            ?>
+                <tr>
+                    <td> <?php echo $value['no'] ?></td>
+                    <td> <?php echo $value['name'] ?></td>
+                    <td> <?php echo $value['age'] ?></td>
+                    <td> <?php echo $value['tel'] ?></td>
+                    <td> <?php echo $value['area'] ?></td>
+                    <td> <?php echo $value['birth'] ?></td>
+                    <td><a href="address_update_front.php?no=<?php echo $value['no'] ?>">수정</a></td>
+                    <td><a href="address_delete.php?no=<?php echo $value['no'] ?>">삭제</a></td>
+                </tr>
+            <?php
+            }
+            ?>
+        </table>
+    </div>
+</body>
+</html>(No newline at end of file)
20220126/dbtest2.php
--- 20220126/dbtest2.php
+++ 20220126/dbtest2.php
@@ -18,27 +18,27 @@
     }
 
 
-    echo "<br><hr>Update Test<br>";
-    $result = $db->updateTest("김현식");
-    if($result) {
-        echo "update Success!";
-    }else {
-        echo "update Fail!";
-    }
+//    echo "<br><hr>Update Test<br>";
+//    $result = $db->updateTest("김현식", "안효원");
+//    if($result) {
+//        echo "update Success!";
+//    }else {
+//        echo "update Fail!";
+//    }
 
 
-    echo "<br><hr>Delete Test<br>";
-    $result = $db->deleteTest("안효원");
-    if($result) {
-        echo "delete Success!";
-    }else {
-        echo "delete Fail!";
-    }
-
-
-
-
-
-
-
-
+//    echo "<br><hr>Delete Test<br>";
+//    $result = $db->deleteTest("안효원");
+//    if($result) {
+//        echo "delete Success!";
+//    }else {
+//        echo "delete Fail!";
+//    }
+//
+//
+//
+//
+//
+//
+//
+//
Add a comment
List