안효원 안효원 2022-02-09
20220209 FILE CRUD Complite
@c5c313cfaab8c7644931e70b3a15716514c1f659
 
20220209/Address.php (added)
+++ 20220209/Address.php
@@ -0,0 +1,86 @@
+<?php
+    require_once("DB.php");
+
+class Address
+{
+    private $db;
+
+    public function __construct(){
+        $db = new DB();
+        $this->db = $db->getDB();
+    }
+
+    public function getListWhere($no) {
+        try {
+            $stmt = $this->db->prepare("select * from address where no = ?");
+            $stmt->execute(array($no));
+            $result = $stmt->fetchAll();
+            return $result;
+        }catch (PDOException $e) {
+            return $e;
+        }
+    }
+
+    public function getList(){
+        try {
+            $stmt = $this->db->prepare("select * from address");
+            $stmt->execute();
+            $result = $stmt->fetchAll();
+            return $result;
+        }catch (Exception $e){
+            echo $e;
+        }
+    }
+
+    public function addressInsert($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(Exception $e){
+            echo $e;
+            return FALSE;
+        }
+    }
+
+    public function addressUpdate($name, $age, $tel, $area, $birth, $no){
+        try {
+            $stmt = $this->db->prepare("update address set name=?, age=?, tel=?, area=?, birth=? where no=?");
+            $stmt->execute(array($name, $age, $tel, $area, $birth, $no));
+            return TRUE;
+        }catch (Exception $e){
+            echo $e;
+            return FALSE;
+        }
+    }
+
+    public function addressDelete($no){
+        try {
+            $stmt = $this->db->prepare("delete from address where no=?");
+            $stmt->execute(array($no));
+            return TRUE;
+        }catch (Exception $e){
+            echo $e;
+            return FALSE;
+        }
+    }
+
+
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 
20220209/DB.php (added)
+++ 20220209/DB.php
@@ -0,0 +1,80 @@
+<?php
+
+class DB
+{
+    private $user = "root";
+    private $password = "1234";
+    private $option = array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
+    private $db;
+
+    public function __construct(){
+        try {
+            $this->db = new PDO("mysql:host=localhost;dbname=study", $this->user, $this->password, $this->option);
+            $this->db->exec("set names UTF8");
+        }catch (PDOException $e){
+            echo "<h2>Database Connection FAIL!!</h2>";
+            echo "Because : " . $e . "<hr>";
+        }
+    }
+
+    public function getDB() {
+        return $this->db;
+    }
+
+//    public function selectTest() {
+//        try {
+//            $stmt = $this->db->prepare("select * from address");
+//            $stmt->execute();
+//            $result = $stmt->fetchAll();
+//            return $result;
+//        } catch (PDOException $e){
+//            return $e;
+//        }
+//    }
+//
+//    public function insertTest(){
+//        try {
+//            $stmt = $this->db->prepare("insert into address values(null, ?, ?, ?, ?, ?)");
+//            $stmt->execute(array("홍길동", 25, '010-5635-5959', '울산', '1789-05-04'));
+//            return TRUE;
+//        }catch(Exception $e){
+//            echo $e;
+//            return FALSE;
+//        }
+//    }
+//
+//    public function updateTest(){
+//        try {
+//            $stmt = $this->db->prepare("update address set name=? where name=?");
+//            $stmt->execute(array('태연', '홍길동'));
+//            return TRUE;
+//        }catch (Exception $e){
+//            echo $e;
+//            return FALSE;
+//        }
+//    }
+//
+//    public function deleteTest(){
+//        try {
+//            $stmt = $this->db->prepare("delete from address where name=?");
+//            $stmt->execute(array('태연'));
+//            return TRUE;
+//        }catch (Exception $e){
+//            echo $e;
+//            return FALSE;
+//        }
+//    }
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
 
20220209/address_insert.php (added)
+++ 20220209/address_insert.php
@@ -0,0 +1,20 @@
+<?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->addressInsert($name, $age, $tel, $area, $birth);
+
+    if($result){
+        echo "<script>alert('Insert Success!')</script>";
+        echo "<script>location.href='address_view.php'</script>";
+    }else {
+        echo "<script>alert('Insert Fail!')</script>";
+        echo "<script>location.href='address_view.php'</script>";
+    }
 
20220209/address_view.php (added)
+++ 20220209/address_view.php
@@ -0,0 +1,85 @@
+<?php
+    require_once("Address.php");
+    $address = new Address();
+    $list = $address->getList();
+?>
+<!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>
+        * {margin: 0 auto; padding: 0; text-align: center;}
+        td, th {padding: 10px;}
+        a {text-decoration: none; color: red;}
+        a:hover {background-color: tomato; color: black;}
+    </style>
+</head>
+<body>
+    <h1>주소록 사이트</h1>
+    <hr>
+    <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>
+            <label for="area">
+                지역 :
+                <select name="area">
+                    <option value="광주">광주</option>
+                    <option value="서울">서울</option>
+                    <option value="부산">부산</option>
+                </select>
+            </label>
+            <label for="birth">
+                생년월일 : <input type="date" name="birth">
+            </label>
+            <input type="submit" value="확인">
+        </form>
+    </div>
+    <table border="1">
+        <tr>
+            <th>번호</th>
+            <th>이름</th>
+            <th>나이</th>
+            <th>연락처</th>
+            <th>지역</th>
+            <th>생년월일</th>
+            <th colspan="2">수정/삭제</th>
+        </tr>
+        <?php foreach ($list as $value){ ?>
+            <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="updateFront.php?no=<?php echo $value['no']; ?>">수정</a></td>
+                <td><a href="delete.php?no=<?php echo $value['no']; ?>">삭제</a></td>
+            </tr>
+        <?php }?>
+    </table>
+</body>
+</html>
+
+
+
+
+
+
+
+
+
+
+
+
 
20220209/dbtest.php (added)
+++ 20220209/dbtest.php
@@ -0,0 +1,14 @@
+<?php
+//    $user = "root";
+//    $password = "1234";
+//    $option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
+//
+//    try {
+//        $db = new PDO("mysql:host=localhost;dbname=study", $user, $password, $option);
+//        $db->exec("set names UTF8");
+//        echo "<strong>Database Connection Success!!!</strong>";
+//    }catch (PDOException $e){
+//        echo "<strong>Database Connection Fail!!!</strong>";
+//        echo "<br>" . $e . "<hr>";
+//    }
+//
 
20220209/dbtest2.php (added)
+++ 20220209/dbtest2.php
@@ -0,0 +1,38 @@
+<?php
+    require_once("DB.php");
+
+    $db = new DB();
+
+    $list = $db->selectTest();
+
+    echo"<br>INSERT TEST<br>";
+    $result = $db->insertTest();
+
+    if($result){
+        echo "insert Success<br>";
+    }else{
+        echo "insert Fail<br>";
+    }
+
+
+    echo"<hr>UPDATE TEST<br>";
+    $result = $db->updateTest();
+
+    if($result){
+        echo "update Success<br>";
+    }else{
+        echo "update Fail<br>";
+    }
+
+    echo"<hr>DELETE TEST<br>";
+    $result = $db->deleteTest();
+
+    if($result){
+        echo "delete Success<br>";
+    }else{
+        echo "delete Fail<br>";
+    }
+    echo "<hr>";
+
+
+    print_r($list);
 
20220209/delete.php (added)
+++ 20220209/delete.php
@@ -0,0 +1,24 @@
+<script>
+    let deleteConfirm = confirm("Are You Sure?");
+</script>
+<?php
+    $deleteConfirm = "<script>document.write(deleteConfirm);</script>";
+    print($deleteConfirm);
+    require_once("Address.php");
+
+    $address = new Address();
+
+
+
+    if($deleteConfirm == TRUE){
+        $result = $address->addressDelete($_GET['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>";
+        }
+    }else{
+
+    }(No newline at end of file)
 
20220209/updateBack.php (added)
+++ 20220209/updateBack.php
@@ -0,0 +1,19 @@
+<?php
+    require_once("Address.php");
+    $address = new Address();
+
+    $name = $_POST['name'];
+    $age = $_POST['age'];
+    $tel = $_POST['tel'];
+    $area = $_POST['area'];
+    $birth = $_POST['birth'];
+
+    $result = $address->addressUpdate($name, $age, $tel, $area, $birth, $_GET['no']);
+
+    if($result){
+        echo "<script>alert('Update Success!!!')</script>";
+        echo "<script>location.href='address_view.php'</script>";
+    }else{
+        echo "<script>alert('Update Fail!!!')</script>";
+        echo "<script>location.href='address_view.php'</script>";
+    }(No newline at end of file)
 
20220209/updateFront.php (added)
+++ 20220209/updateFront.php
@@ -0,0 +1,38 @@
+<?php
+    require_once("Address.php");
+    $address = new Address();
+    $list = $address->getListWhere($_GET['no']);
+?>
+<!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>
+    <style>
+        * {margin: 0 auto; padding: 0; text-align: center;}
+    </style>
+</head>
+<body>
+    <h1>UPDATE</h1>
+    <hr>
+    <div>
+        <form action="updateBack.php?no=<?php echo $_GET['no']; ?>" method="post">
+            이름 : <input type="text" name="name" size="5" value="<?php echo $list[0][1]; ?>">
+            나이 : <input type="text" name="age" size="3" value="<?php echo $list[0][2]; ?>">
+            연락처 : <input type="text" name="tel" size="8" value="<?php echo $list[0][3]; ?>">
+            지역 :
+            <select name="area">
+                <option value="광주" <?php if($list[0][4] == '광주') echo 'selected'; ?>>광주</option>
+                <option value="서울" <?php if($list[0][4] == '서울') echo 'selected'; ?>>서울</option>
+                <option value="부산" <?php if($list[0][4] == '부산') echo 'selected'; ?>>부산</option>
+            </select>
+                생년월일 : <input type="date" name="birth" value="<?php echo $list[0][5]; ?>">
+            <br><input type="submit" value="수정">
+        </form>
+        <button onclick="document.location.href='address_view.php'">취소</button>
+    </div>
+</body>
+</html>(No newline at end of file)
Add a comment
List