1. 게시글 수정
** view.php 변경
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_GET['b_idx'];
$sql = "UPDATE tb_board SET b_hit = b_hit + 1 WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$sql = "SELECT * FROM tb_board WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$id = $_SESSION['id']; // 세션 아이디
$b_userid = $row['b_userid']; // 글쓴이 아이디
$b_title = $row['b_title'];
$b_content = $row['b_content'];
$b_hit = $row['b_hit'];
$b_regdate = $row['b_regdate'];
$b_up = $row['b_up'];
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글보기</title>
</head>
<body>
<h2>글보기</h2>
<p><b>글쓴이</b> : <?=$b_userid?></p>
<p><b>날짜</b> : <?=$b_regdate?></p>
<p><b>조회수</b> : <?=$b_hit?></p>
<p><b>추천수</b> : <?=$b_up?></p>
<p><b>제목</b> : <?=$b_title?></p>
<p><b>내용</b></p>
<p><?=$b_content?></p>
<p><input type="button" value="리스트" onclick="location.href='./list.php'">
<?php
if($id == $b_userid) {
?>
<input type="button" value="수정" onclick="location.href='./edit.php?b_idx=<?=$b_idx?>'">
<input type="button" value="삭제" onclick="location.href='./delete.php'">
<?php
}
?>
</p>
</body>
</html>
** edit.php
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_GET['b_idx'];
$sql = "SELECT * FROM tb_board WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$b_title = $row['b_title'];
$b_content = $row['b_content'];
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글수정</title>
</head>
<body>
<h2>글수정</h2>
<form method="post" action="edit_ok.php">
<input type="hidden" name="b_idx" value="<?=$b_idx?>">
<p><label>아이디 : <?=$_SESSION['id']?></label></p>
<p><label>제목 : <input type="text" name="b_title" value="<?=$b_title?>"></label></p>
<p>내용</p>
<p><textarea cols="40" rows="6" name="b_content"><?=$b_content?></textarea></p>
<p><input type="submit" value="확인"> <input type="reset" value="다시작성">
<input type="button" value="리스트" onclick="location.href='./list.php'"></p>
</form>
</body>
</html>
** edit_ok.php
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_POST['b_idx'];
$b_title = $_POST['b_title'];
$b_content = $_POST['b_content'];
//echo $b_idx."<br>";
//echo $b_title."<br>";
//echo $b_content."<br>";
$sql = "UPDATE tb_board SET b_title='$b_title', b_content='$b_content' WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
echo "<script>alert('수정되었습니다.');location.href='view.php?b_idx=".$b_idx."';</script>"
?>
2. 게시글 삭제
** view.php 변경
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_GET['b_idx'];
$sql = "UPDATE tb_board SET b_hit = b_hit + 1 WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$sql = "SELECT * FROM tb_board WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$id = $_SESSION['id']; // 세션 아이디
$b_userid = $row['b_userid']; // 글쓴이 아이디
$b_title = $row['b_title'];
$b_content = $row['b_content'];
$b_hit = $row['b_hit'];
$b_regdate = $row['b_regdate'];
$b_up = $row['b_up'];
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글보기</title>
</head>
<body>
<h2>글보기</h2>
<p><b>글쓴이</b> : <?=$b_userid?></p>
<p><b>날짜</b> : <?=$b_regdate?></p>
<p><b>조회수</b> : <?=$b_hit?></p>
<p><b>추천수</b> : <?=$b_up?></p>
<p><b>제목</b> : <?=$b_title?></p>
<p><b>내용</b></p>
<p><?=$b_content?></p>
<p><input type="button" value="리스트" onclick="location.href='./list.php'">
<?php
if($id == $b_userid) {
?>
<input type="button" value="수정" onclick="location.href='./edit.php?b_idx=<?=$b_idx?>'">
<input type="button" value="삭제" onclick="location.href='./delete.php?b_idx=<?=$b_idx?>'">
<?php
}
?>
</p>
</body>
</html>
** delete.php
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_GET['b_idx'];
$sql = "DELETE FROM tb_board WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
echo "<script>alert('삭제되었습니다.');location.href='list.php';</script>";
?>
3. 첨부파일 추가 글쓰기 변경
** upload 폴더 생성
** write.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글쓰기</title>
</head>
<body>
<h2>글쓰기</h2>
<form method="post" action="write_ok.php" enctype="multipart/form-data">
<p><label>아이디 : <?=$_SESSION['id']?></label></p>
<p><label>제목 : <input type="text" name="b_title"></label></p>
<p>내용</p>
<p><textarea cols="40" rows="6" name="b_content"></textarea></p>
<p>파일 : <input type="file" name="b_file"></p>
<p><input type="submit" value="확인">
<input type="reset" value="다시작성">
<input type="button" value="리스트" onclick="location.href='./list.php'"></p>
</form>
</body>
</html>
** write_ok.php
<?php
session_start();
include "../include/dbconn.php";
$id = $_SESSION['id'];
$b_title = $_POST['b_title'];
$b_content = $_POST['b_content'];
$filepath = "";
if($_FILES['b_file']['tmp_name']) {
$uploads_dir = './upload';
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
$error = $_FILES['b_file']['error'];
$name = $_FILES['b_file']['name']; // apple.jpg
$ext = explode(".", $name); // ext[0] = "apple", ext[1] = "jpg"
$rename = $ext[0].time();
$rename = $rename.".".$ext[1];
$ext = strtolower(array_pop($ext)); // array_pop() : 스택구조,
// 마지막 값을 뽑아내고 그 값을 반환한다.
// 해당 데이터는 array에서 사라진다.
if($error != UPLOAD_ERR_OK) { // UPLOAD_ERR_OK(0) -> 오류 없이 파일 업로드 성공
switch($error) {
case UPLOAD_ERR_INI_SIZE: // php.ini에 설정된 최대 파일을 초과
case UPLOAD_ERR_FORM_SIZE: // HTML 폼에 설정된 최대 파일을 초과
echo "파일 크기가 너무 큽니다.";
break;
case UPLOAD_ERR_NO_FILE: // 파일이 제대로 업로드 되지 않았을 경우
echo "파일이 제대로 첨부되지 않았습니다.";
break;
default:
echo "파일 업로드 실패";
}
exit;
}
if(!in_array($ext, $allowed_ext)) {
echo "허용되지 않은 확장명입니다.";
exit;
}
$filepath = $uploads_dir."/".$rename;
move_uploaded_file($_FILES['b_file']['tmp_name'], $filepath);
}
$sql = "INSERT INTO tb_board (b_userid, b_title, b_content, b_file) VALUES ('$id', '$b_title', '$b_content', '$filepath')";
$result = mysqli_query($conn, $sql);
echo "<script>alert('저장되었습니다.');location.href='list.php';</script>"
?>
4. 처음에 첨부파일 추가 글쓰기 변경 만들때,
너무 큰 이미지 파일을 사용하여 아파치에서부터 막아서 제대로 insert가 되지 않았다.
만약 대용량의 이미지 첨부가 필요하면 아파치에서 사이즈 증가시키면 된다고 한다.
5. 첨부파일 여부 확인하여 리스트에 표시하기
** list.php
<?php
include "../include/dbconn.php";
$sql = "SELECT * FROM tb_board ORDER BY b_idx DESC";
$result = mysqli_query($conn, $sql);
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>리스트</title>
</head>
<body>
<h2>리스트</h2>
<table border="1" width="800">
<tr>
<th>번호</th>
<th>제목</th>
<th>글쓴이</th>
<th>조회수</th>
<th>날짜</th>
<th>추천수</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$b_idx = $row['b_idx'];
$b_userid = $row['b_userid'];
$b_title = $row['b_title'];
$b_hit = $row['b_hit'];
$b_regdate = $row['b_regdate'];
$b_up = $row['b_up'];
$b_file = $row['b_file'];
$imgpath = "";
if($row['b_file'] != "") {
$imgpath = "<img src='../images/file.png' width='16' alt='file'>";
}
?>
<tr>
<td><?=$b_idx?></td>
<td><a href="./view.php?b_idx=<?=$b_idx?>"><?=$b_title?></a><?=$imgpath?></td>
<td><?=$b_userid?></td>
<td><?=$b_hit?></td>
<td><?=$b_regdate?></td>
<td><?=$b_up?></td>
</tr>
<?php
}
?>
</table>
<p><input type="button" value="글쓰기" onclick="location.href='./write.php'">
<input type="button" value="돌아가기" onclick="location.href='../login.php'"></p>
</body>
</html>
6. 글 상세보기에 등록했던 이미지 보여주기
** view.php
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_GET['b_idx'];
$sql = "UPDATE tb_board SET b_hit = b_hit + 1 WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$sql = "SELECT * FROM tb_board WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$id = $_SESSION['id']; // 세션 아이디
$b_userid = $row['b_userid']; // 글쓴이 아이디
$b_title = $row['b_title'];
$b_content = $row['b_content'];
$b_hit = $row['b_hit'];
$b_regdate = $row['b_regdate'];
$b_up = $row['b_up'];
$b_file = $row['b_file'];
$imgpath = "";
if($row['b_file'] != "") {
$imgpath = "<img src='".$b_file."' width='200' alt='file'>";
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글보기</title>
</head>
<body>
<h2>글보기</h2>
<p><b>글쓴이</b> : <?=$b_userid?></p>
<p><b>날짜</b> : <?=$b_regdate?></p>
<p><b>조회수</b> : <?=$b_hit?></p>
<p><b>추천수</b> : <?=$b_up?></p>
<p><b>제목</b> : <?=$b_title?></p>
<p><b>내용</b></p>
<p><?=$b_content?></p>
<p><?=$imgpath?></p>
<p><input type="button" value="리스트" onclick="location.href='./list.php'">
<?php
if($id == $b_userid) {
?>
<input type="button" value="수정" onclick="location.href='./edit.php?b_idx=<?=$b_idx?>'">
<input type="button" value="삭제" onclick="location.href='./delete.php?b_idx=<?=$b_idx?>'">
<?php
}
?>
</p>
</body>
</html>
7. 첨부파일 추가한 게시글 수정
** edit.php
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_GET['b_idx'];
$sql = "SELECT * FROM tb_board WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$b_title = $row['b_title'];
$b_content = $row['b_content'];
$b_file = $row['b_file'];
$imgpath = "";
if($row['b_file'] != "") {
$imgpath = "<img src='".$b_file."' width='200' alt='file'>";
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글수정</title>
</head>
<body>
<h2>글수정</h2>
<form method="post" action="edit_ok.php" enctype="multipart/form-data">
<input type="hidden" name="b_idx" value="<?=$b_idx?>">
<p><label>아이디 : <?=$_SESSION['id']?></label></p>
<p><label>제목 : <input type="text" name="b_title" value="<?=$b_title?>"></label></p>
<p>내용</p>
<p><textarea cols="40" rows="6" name="b_content"><?=$b_content?></textarea></p>
<p>파일 : <input type="file" name="b_file"></p>
<p><?=$imgpath?></p>
<p><input type="submit" value="확인"> <input type="reset" value="다시작성">
<input type="button" value="리스트" onclick="location.href='./list.php'"></p>
</form>
</body>
</html>
** edit_ok.php
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_POST['b_idx'];
$b_title = $_POST['b_title'];
$b_content = $_POST['b_content'];
$filepath = "";
if($_FILES['b_file']['tmp_name']) {
$uploads_dir = './upload';
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
$error = $_FILES['b_file']['error'];
$name = $_FILES['b_file']['name'];
$ext = explode(".", $name);
$rename = $ext[0].time();
$rename = $rename.".".$ext[1];
$ext = strtolower(array_pop($ext));
if($error != UPLOAD_ERR_OK) {
switch($error) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
echo "파일 크기가 너무 큽니다.";
break;
case UPLOAD_ERR_NO_FILE:
echo "파일이 제대로 첨부되지 않았습니다.";
break;
default:
echo "파일 업로드 실패";
}
exit;
}
if(!in_array($ext, $allowed_ext)) {
echo "허용되지 않은 확장명입니다.";
exit;
}
$filepath = $uploads_dir."/".$rename;
move_uploaded_file($_FILES['b_file']['tmp_name'], $filepath);
// 첨부파일이 있을때
$sql = "UPDATE tb_board SET b_title='$b_title', b_content='$b_content', b_file='$filepath' WHERE b_idx=$b_idx";
} else {
// 첨부파일이 없을때
$sql = "UPDATE tb_board SET b_title='$b_title', b_content='$b_content' WHERE b_idx=$b_idx";
}
$result = mysqli_query($conn, $sql);
echo "<script>alert('수정되었습니다.');location.href='view.php?b_idx=".$b_idx."';</script>"
?>
8. 댓글
** view.php
<?php
session_start();
include "../include/dbconn.php";
$b_idx = $_GET['b_idx'];
$sql = "UPDATE tb_board SET b_hit = b_hit + 1 WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$sql = "SELECT * FROM tb_board WHERE b_idx=$b_idx";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$id = $_SESSION['id']; // 세션 아이디
$b_userid = $row['b_userid']; // 글쓴이 아이디
$b_title = $row['b_title'];
$b_content = $row['b_content'];
$b_hit = $row['b_hit'];
$b_regdate = $row['b_regdate'];
$b_up = $row['b_up'];
$b_file = $row['b_file'];
$imgpath = "";
if($row['b_file'] != "") {
$imgpath = "<img src='".$b_file."' width='200' alt='file'>";
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>글보기</title>
</head>
<body>
<h2>글보기</h2>
<p><b>글쓴이</b> : <?=$b_userid?></p>
<p><b>날짜</b> : <?=$b_regdate?></p>
<p><b>조회수</b> : <?=$b_hit?></p>
<p><b>추천수</b> : <?=$b_up?></p>
<p><b>제목</b> : <?=$b_title?></p>
<p><b>내용</b></p>
<p><?=$b_content?></p>
<p><?=$imgpath?></p>
<p><input type="button" value="리스트" onclick="location.href='./list.php'">
<?php
if($id == $b_userid) {
?>
<input type="button" value="수정" onclick="location.href='./edit.php?b_idx=<?=$b_idx?>'">
<input type="button" value="삭제" onclick="location.href='./delete.php?b_idx=<?=$b_idx?>'">
<?php
}
?>
</p>
<form name="reform" method="post" action="reply_ok.php">
<input type="hidden" name="b_idx" value="<?=$b_idx?>">
<p><?=$id?> : <input type="text" name="re_content"> <input type="submit" value="댓글달기"></p>
</form>
<hr>
<?php
$sql = "SELECT * FROM tb_reply WHERE re_board_idx='$b_idx' ORDER BY re_idx DESC";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)) {
$re_idx = $row['re_idx'];
$re_userid = $row['re_userid'];
$re_content = $row['re_content'];
$re_regdate = $row['re_regdate'];
$re_board_idx = $row['re_board_idx']
?>
<p><?=$re_userid?> - <?=$re_content?> (<?=$re_regdate?>)
<?php
if($id == $re_userid) {
?>
<input type="button" value="삭제"
onclick="location.href='reply_del.php?re_board_idx=<?=$re_board_idx?>&re_idx=<?=$re_idx?>'"></p>
<?php
}
}
?>
</body>
</html>
** reply_ok.php
<?php
session_start();
include "../include/dbconn.php";
$id = $_SESSION['id'];
$re_content = $_POST['re_content'];
$b_idx = $_POST['b_idx'];
$sql = "INSERT INTO tb_reply(re_userid, re_content, re_board_idx) VALUES ('$id', '$re_content', '$b_idx')";
$result = mysqli_query($conn, $sql);
echo "<script>alert('저장되었습니다.');location.href='view.php?b_idx=".$b_idx."'</script>";
?>
** reply_del.php
<?php
session_start();
include "../include/dbconn.php";
$re_board_idx = $_GET['re_board_idx'];
$re_idx = $_GET['re_idx'];
$sql = "DELETE FROM tb_reply WHERE re_idx='$re_idx'";
$result = mysqli_query($conn, $sql);
echo "<script>alert('삭제되었습니다.');location.href='view.php?b_idx=".$re_board_idx."';</script>";
?>
'웹_프론트_백엔드 > 프론트엔드' 카테고리의 다른 글
2021.02.20 (0) | 2021.02.21 |
---|---|
2021.02.07 (0) | 2021.02.18 |
2021.01.31 (0) | 2021.01.31 |
2021.01.30 (0) | 2021.01.30 |
2021.01.24 (0) | 2021.01.27 |