+++ 20220209/Address.php
... | ... | @@ -0,0 +1,86 @@ |
1 | +<?php | |
2 | + require_once("DB.php"); | |
3 | + | |
4 | +class Address | |
5 | +{ | |
6 | + private $db; | |
7 | + | |
8 | + public function __construct(){ | |
9 | + $db = new DB(); | |
10 | + $this->db = $db->getDB(); | |
11 | + } | |
12 | + | |
13 | + public function getListWhere($no) { | |
14 | + try { | |
15 | + $stmt = $this->db->prepare("select * from address where no = ?"); | |
16 | + $stmt->execute(array($no)); | |
17 | + $result = $stmt->fetchAll(); | |
18 | + return $result; | |
19 | + }catch (PDOException $e) { | |
20 | + return $e; | |
21 | + } | |
22 | + } | |
23 | + | |
24 | + public function getList(){ | |
25 | + try { | |
26 | + $stmt = $this->db->prepare("select * from address"); | |
27 | + $stmt->execute(); | |
28 | + $result = $stmt->fetchAll(); | |
29 | + return $result; | |
30 | + }catch (Exception $e){ | |
31 | + echo $e; | |
32 | + } | |
33 | + } | |
34 | + | |
35 | + public function addressInsert($name, $age, $tel, $area, $birth){ | |
36 | + try { | |
37 | + $stmt = $this->db->prepare("insert into address values(null, ?, ?, ?, ?, ?)"); | |
38 | + $stmt->execute(array($name, $age, $tel, $area, $birth)); | |
39 | + return TRUE; | |
40 | + }catch(Exception $e){ | |
41 | + echo $e; | |
42 | + return FALSE; | |
43 | + } | |
44 | + } | |
45 | + | |
46 | + public function addressUpdate($name, $age, $tel, $area, $birth, $no){ | |
47 | + try { | |
48 | + $stmt = $this->db->prepare("update address set name=?, age=?, tel=?, area=?, birth=? where no=?"); | |
49 | + $stmt->execute(array($name, $age, $tel, $area, $birth, $no)); | |
50 | + return TRUE; | |
51 | + }catch (Exception $e){ | |
52 | + echo $e; | |
53 | + return FALSE; | |
54 | + } | |
55 | + } | |
56 | + | |
57 | + public function addressDelete($no){ | |
58 | + try { | |
59 | + $stmt = $this->db->prepare("delete from address where no=?"); | |
60 | + $stmt->execute(array($no)); | |
61 | + return TRUE; | |
62 | + }catch (Exception $e){ | |
63 | + echo $e; | |
64 | + return FALSE; | |
65 | + } | |
66 | + } | |
67 | + | |
68 | + | |
69 | + | |
70 | +} | |
71 | + | |
72 | + | |
73 | + | |
74 | + | |
75 | + | |
76 | + | |
77 | + | |
78 | + | |
79 | + | |
80 | + | |
81 | + | |
82 | + | |
83 | + | |
84 | + | |
85 | + | |
86 | + |
+++ 20220209/DB.php
... | ... | @@ -0,0 +1,80 @@ |
1 | +<?php | |
2 | + | |
3 | +class DB | |
4 | +{ | |
5 | + private $user = "root"; | |
6 | + private $password = "1234"; | |
7 | + private $option = array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION); | |
8 | + private $db; | |
9 | + | |
10 | + public function __construct(){ | |
11 | + try { | |
12 | + $this->db = new PDO("mysql:host=localhost;dbname=study", $this->user, $this->password, $this->option); | |
13 | + $this->db->exec("set names UTF8"); | |
14 | + }catch (PDOException $e){ | |
15 | + echo "<h2>Database Connection FAIL!!</h2>"; | |
16 | + echo "Because : " . $e . "<hr>"; | |
17 | + } | |
18 | + } | |
19 | + | |
20 | + public function getDB() { | |
21 | + return $this->db; | |
22 | + } | |
23 | + | |
24 | +// public function selectTest() { | |
25 | +// try { | |
26 | +// $stmt = $this->db->prepare("select * from address"); | |
27 | +// $stmt->execute(); | |
28 | +// $result = $stmt->fetchAll(); | |
29 | +// return $result; | |
30 | +// } catch (PDOException $e){ | |
31 | +// return $e; | |
32 | +// } | |
33 | +// } | |
34 | +// | |
35 | +// public function insertTest(){ | |
36 | +// try { | |
37 | +// $stmt = $this->db->prepare("insert into address values(null, ?, ?, ?, ?, ?)"); | |
38 | +// $stmt->execute(array("홍길동", 25, '010-5635-5959', '울산', '1789-05-04')); | |
39 | +// return TRUE; | |
40 | +// }catch(Exception $e){ | |
41 | +// echo $e; | |
42 | +// return FALSE; | |
43 | +// } | |
44 | +// } | |
45 | +// | |
46 | +// public function updateTest(){ | |
47 | +// try { | |
48 | +// $stmt = $this->db->prepare("update address set name=? where name=?"); | |
49 | +// $stmt->execute(array('태연', '홍길동')); | |
50 | +// return TRUE; | |
51 | +// }catch (Exception $e){ | |
52 | +// echo $e; | |
53 | +// return FALSE; | |
54 | +// } | |
55 | +// } | |
56 | +// | |
57 | +// public function deleteTest(){ | |
58 | +// try { | |
59 | +// $stmt = $this->db->prepare("delete from address where name=?"); | |
60 | +// $stmt->execute(array('태연')); | |
61 | +// return TRUE; | |
62 | +// }catch (Exception $e){ | |
63 | +// echo $e; | |
64 | +// return FALSE; | |
65 | +// } | |
66 | +// } | |
67 | + | |
68 | +} | |
69 | + | |
70 | + | |
71 | + | |
72 | + | |
73 | + | |
74 | + | |
75 | + | |
76 | + | |
77 | + | |
78 | + | |
79 | + | |
80 | + |
+++ 20220209/address_insert.php
... | ... | @@ -0,0 +1,20 @@ |
1 | +<?php | |
2 | + require_once("Address.php"); | |
3 | + | |
4 | + $name = $_POST['name']; | |
5 | + $age = $_POST['age']; | |
6 | + $tel = $_POST['tel']; | |
7 | + $area = $_POST['area']; | |
8 | + $birth = $_POST['birth']; | |
9 | + | |
10 | + $address = new Address(); | |
11 | + | |
12 | + $result = $address->addressInsert($name, $age, $tel, $area, $birth); | |
13 | + | |
14 | + if($result){ | |
15 | + echo "<script>alert('Insert Success!')</script>"; | |
16 | + echo "<script>location.href='address_view.php'</script>"; | |
17 | + }else { | |
18 | + echo "<script>alert('Insert Fail!')</script>"; | |
19 | + echo "<script>location.href='address_view.php'</script>"; | |
20 | + } |
+++ 20220209/address_view.php
... | ... | @@ -0,0 +1,85 @@ |
1 | +<?php | |
2 | + require_once("Address.php"); | |
3 | + $address = new Address(); | |
4 | + $list = $address->getList(); | |
5 | +?> | |
6 | +<!doctype html> | |
7 | +<html lang="ko"> | |
8 | +<head> | |
9 | + <meta charset="UTF-8"> | |
10 | + <meta name="viewport" | |
11 | + content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
12 | + <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
13 | + <title>조소록</title> | |
14 | + <style> | |
15 | + * {margin: 0 auto; padding: 0; text-align: center;} | |
16 | + td, th {padding: 10px;} | |
17 | + a {text-decoration: none; color: red;} | |
18 | + a:hover {background-color: tomato; color: black;} | |
19 | + </style> | |
20 | +</head> | |
21 | +<body> | |
22 | + <h1>주소록 사이트</h1> | |
23 | + <hr> | |
24 | + <div> | |
25 | + <form action="address_insert.php" method="post"> | |
26 | + <label for="name"> | |
27 | + 이름 : <input type="text" name="name" size="5"> | |
28 | + </label> | |
29 | + <label for="age"> | |
30 | + 나이 : <input type="text" name="age" size="2"> | |
31 | + </label> | |
32 | + <label for="tel"> | |
33 | + 연락처 : <input type="text" name="tel" size="8"> | |
34 | + </label> | |
35 | + <label for="area"> | |
36 | + 지역 : | |
37 | + <select name="area"> | |
38 | + <option value="광주">광주</option> | |
39 | + <option value="서울">서울</option> | |
40 | + <option value="부산">부산</option> | |
41 | + </select> | |
42 | + </label> | |
43 | + <label for="birth"> | |
44 | + 생년월일 : <input type="date" name="birth"> | |
45 | + </label> | |
46 | + <input type="submit" value="확인"> | |
47 | + </form> | |
48 | + </div> | |
49 | + <table border="1"> | |
50 | + <tr> | |
51 | + <th>번호</th> | |
52 | + <th>이름</th> | |
53 | + <th>나이</th> | |
54 | + <th>연락처</th> | |
55 | + <th>지역</th> | |
56 | + <th>생년월일</th> | |
57 | + <th colspan="2">수정/삭제</th> | |
58 | + </tr> | |
59 | + <?php foreach ($list as $value){ ?> | |
60 | + <tr> | |
61 | + <td><?php echo $value['no']?></td> | |
62 | + <td><?php echo $value['name']?></td> | |
63 | + <td><?php echo $value['age']?></td> | |
64 | + <td><?php echo $value['tel']?></td> | |
65 | + <td><?php echo $value['area']?></td> | |
66 | + <td><?php echo $value['birth']?></td> | |
67 | + <td><a href="updateFront.php?no=<?php echo $value['no']; ?>">수정</a></td> | |
68 | + <td><a href="delete.php?no=<?php echo $value['no']; ?>">삭제</a></td> | |
69 | + </tr> | |
70 | + <?php }?> | |
71 | + </table> | |
72 | +</body> | |
73 | +</html> | |
74 | + | |
75 | + | |
76 | + | |
77 | + | |
78 | + | |
79 | + | |
80 | + | |
81 | + | |
82 | + | |
83 | + | |
84 | + | |
85 | + |
+++ 20220209/dbtest.php
... | ... | @@ -0,0 +1,14 @@ |
1 | +<?php | |
2 | +// $user = "root"; | |
3 | +// $password = "1234"; | |
4 | +// $option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); | |
5 | +// | |
6 | +// try { | |
7 | +// $db = new PDO("mysql:host=localhost;dbname=study", $user, $password, $option); | |
8 | +// $db->exec("set names UTF8"); | |
9 | +// echo "<strong>Database Connection Success!!!</strong>"; | |
10 | +// }catch (PDOException $e){ | |
11 | +// echo "<strong>Database Connection Fail!!!</strong>"; | |
12 | +// echo "<br>" . $e . "<hr>"; | |
13 | +// } | |
14 | +// |
+++ 20220209/dbtest2.php
... | ... | @@ -0,0 +1,38 @@ |
1 | +<?php | |
2 | + require_once("DB.php"); | |
3 | + | |
4 | + $db = new DB(); | |
5 | + | |
6 | + $list = $db->selectTest(); | |
7 | + | |
8 | + echo"<br>INSERT TEST<br>"; | |
9 | + $result = $db->insertTest(); | |
10 | + | |
11 | + if($result){ | |
12 | + echo "insert Success<br>"; | |
13 | + }else{ | |
14 | + echo "insert Fail<br>"; | |
15 | + } | |
16 | + | |
17 | + | |
18 | + echo"<hr>UPDATE TEST<br>"; | |
19 | + $result = $db->updateTest(); | |
20 | + | |
21 | + if($result){ | |
22 | + echo "update Success<br>"; | |
23 | + }else{ | |
24 | + echo "update Fail<br>"; | |
25 | + } | |
26 | + | |
27 | + echo"<hr>DELETE TEST<br>"; | |
28 | + $result = $db->deleteTest(); | |
29 | + | |
30 | + if($result){ | |
31 | + echo "delete Success<br>"; | |
32 | + }else{ | |
33 | + echo "delete Fail<br>"; | |
34 | + } | |
35 | + echo "<hr>"; | |
36 | + | |
37 | + | |
38 | + print_r($list); |
+++ 20220209/delete.php
... | ... | @@ -0,0 +1,24 @@ |
1 | +<script> | |
2 | + let deleteConfirm = confirm("Are You Sure?"); | |
3 | +</script> | |
4 | +<?php | |
5 | + $deleteConfirm = "<script>document.write(deleteConfirm);</script>"; | |
6 | + print($deleteConfirm); | |
7 | + require_once("Address.php"); | |
8 | + | |
9 | + $address = new Address(); | |
10 | + | |
11 | + | |
12 | + | |
13 | + if($deleteConfirm == TRUE){ | |
14 | + $result = $address->addressDelete($_GET['no']); | |
15 | + if($result) { | |
16 | + echo "<script>alert('Delete Success!!!')</script>"; | |
17 | + echo "<script>document.location.href='address_view.php'</script>"; | |
18 | + }else { | |
19 | + echo "<script>alert('Delete Fail!!!')</script>"; | |
20 | + echo "<script>document.location.href='address_view.php'</script>"; | |
21 | + } | |
22 | + }else{ | |
23 | + | |
24 | + }(No newline at end of file) |
+++ 20220209/updateBack.php
... | ... | @@ -0,0 +1,19 @@ |
1 | +<?php | |
2 | + require_once("Address.php"); | |
3 | + $address = new Address(); | |
4 | + | |
5 | + $name = $_POST['name']; | |
6 | + $age = $_POST['age']; | |
7 | + $tel = $_POST['tel']; | |
8 | + $area = $_POST['area']; | |
9 | + $birth = $_POST['birth']; | |
10 | + | |
11 | + $result = $address->addressUpdate($name, $age, $tel, $area, $birth, $_GET['no']); | |
12 | + | |
13 | + if($result){ | |
14 | + echo "<script>alert('Update Success!!!')</script>"; | |
15 | + echo "<script>location.href='address_view.php'</script>"; | |
16 | + }else{ | |
17 | + echo "<script>alert('Update Fail!!!')</script>"; | |
18 | + echo "<script>location.href='address_view.php'</script>"; | |
19 | + }(No newline at end of file) |
+++ 20220209/updateFront.php
... | ... | @@ -0,0 +1,38 @@ |
1 | +<?php | |
2 | + require_once("Address.php"); | |
3 | + $address = new Address(); | |
4 | + $list = $address->getListWhere($_GET['no']); | |
5 | +?> | |
6 | +<!doctype html> | |
7 | +<html lang="ko"> | |
8 | +<head> | |
9 | + <meta charset="UTF-8"> | |
10 | + <meta name="viewport" | |
11 | + content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | |
12 | + <meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
13 | + <title>Document</title> | |
14 | + <style> | |
15 | + * {margin: 0 auto; padding: 0; text-align: center;} | |
16 | + </style> | |
17 | +</head> | |
18 | +<body> | |
19 | + <h1>UPDATE</h1> | |
20 | + <hr> | |
21 | + <div> | |
22 | + <form action="updateBack.php?no=<?php echo $_GET['no']; ?>" method="post"> | |
23 | + 이름 : <input type="text" name="name" size="5" value="<?php echo $list[0][1]; ?>"> | |
24 | + 나이 : <input type="text" name="age" size="3" value="<?php echo $list[0][2]; ?>"> | |
25 | + 연락처 : <input type="text" name="tel" size="8" value="<?php echo $list[0][3]; ?>"> | |
26 | + 지역 : | |
27 | + <select name="area"> | |
28 | + <option value="광주" <?php if($list[0][4] == '광주') echo 'selected'; ?>>광주</option> | |
29 | + <option value="서울" <?php if($list[0][4] == '서울') echo 'selected'; ?>>서울</option> | |
30 | + <option value="부산" <?php if($list[0][4] == '부산') echo 'selected'; ?>>부산</option> | |
31 | + </select> | |
32 | + 생년월일 : <input type="date" name="birth" value="<?php echo $list[0][5]; ?>"> | |
33 | + <br><input type="submit" value="수정"> | |
34 | + </form> | |
35 | + <button onclick="document.location.href='address_view.php'">취소</button> | |
36 | + </div> | |
37 | +</body> | |
38 | +</html>(No newline at end of file) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?