1. 게시글 수정 폼: update_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8"/>
<title> Update </title>
</head>
<body>
<form method="POST" action="./update.php">
<table align="center" border="1" width="850px">
<?php
include "./connectDB.php";
$conn = connectDB();
if(isset($_GET["postNum"])) {
//숫자가 아니라면
if(preg_match("/[^0-9]+/", $_GET["postNum"])) {
?>
<script>
alert("No hack~ \'~\'");
history.back();
</script>
<?php
} else {
$postNum = $_GET["postNum"];
}
} else {
?>
<script>
alert("오류");
history.back();
</script>
<?php
}
$query = "SELECT writer, subject, content FROM post WHERE seq=$postNum";
$result = mysqli_query($conn, $query);
$arr = mysqli_fetch_array($result, MYSQLI_ASSOC);
$writer = $arr["writer"];
$subject = $arr["subject"];
$content = $arr["content"];
?>
<tr>
<td align="center" width="100px"> 제목 </td>
<td align="left" width="550px">
<input type="text" name="sub" maxlength="122" style="width:540px"value="<?=$subject?>"/>
</td>
<td align="center" width="200px"> <?=$writer?> </td>
</tr>
<tr>
<td colspan="3" align="center">
<textarea type="text" name="cont" style="width:850px; height:600px; resize:none;"><?=$content?></textarea>
</td>
</tr>
</table>
</br>
<div align="center">
<input type="submit" value="확인" />
<input type="hidden" name="seq" value=<?=$postNum?> />
</div>
</form>
</body>
</html>
|
cs |
16행: 수정 버튼을 눌러 이 페이지로 이동될 때는 GET 방식으로 수정할 글의 번호(seq)가 전달된다.
33행: 이 때 이 GET 값이 정의되어 있지 않으면 이전 페이지로 이동한다.
38행 ~ 44행: 전달받은 글 번호로 글의 작성자명, 제목, 내용을 불러온다.
45행 ~ 57행: 불러온 내용을 게시글 작성 폼에 띄운다.
59행 ~ 62행: 확인 버튼을 누르면 seq라는 이름으로 수정한 글 번호가 update.php로 전달된다.
이 때 9행의 method="POST" 에 의해 POST 방식으로 전달된다.
2. 게시글 수정: update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?php
include "./connectDB.php";
$conn = connectDB();
$inputSub = $_POST["sub"];
$inputCont = $_POST["cont"];
$inputSeq = $_POST["seq"];
if($inputSub === "" || $inputCont === "") {
?>
<script>
alert("공백 불가");
history.back();
</script>
<?php
exit();
}
$inputSub = htmlspecialchars($inputSub);
$inputCont = htmlspecialchars($inputCont);
$query = "UPDATE post SET subject=\"$inputSub\", content=\"$inputCont\" WHERE seq=$inputSeq";
$result = mysqli_query($conn, $query);
if($result) {
?>
<script>
alert("수정되었습니다.");
document.location.href="./list.php";
</script>
<?php
} else {
?>
<script>
alert("수정이 실패했습니다.");
document.location.href="./list.php";
</script>
<?php
}
?>
|
cs |
6행 ~ 8행: update_form.php에서 입력한 데이터(제목, 내용, 수정할 글 번호)를 변수에 저장하고
23행: 그 값을 토대로 DB를 수정한다.
26행 ~ 39행: 23행의 쿼리 실행 결과에 따라 수정 성공 / 실패 문구를 띄우고 게시글 목록 페이지로 이동한다.
근데 이렇게 같은 문장을 두 번 쓸 거면 document.location.href="./list.php"를 아예 if-else 문 밖으로
빼는 게 나을 것 같다.
'Programming > Web' 카테고리의 다른 글
[게시판] 게시글 목록 (0) | 2019.07.18 |
---|---|
[게시판] 게시글 삭제 (0) | 2019.07.18 |
[게시판] 게시글 작성 (0) | 2019.07.16 |
[게시판] 회원가입 (0) | 2019.07.16 |
[게시판] DB 연결, 로그인 세션 설정 + 로그인 (0) | 2019.07.12 |