[강좌] php+mysql 게시판 만들기 #쉬-5
안녕하세요. 디망쉬입니다.
움냐아. 요즘 사람을 너무 믿어서는 안된다는 교훈을 자주 깨닫습니다. 이전에는 머리로만 '너무 믿어선 안돼. 안돼' 라며 경계했지만 이제는 마음으로 '너무 믿으면 안돼' 라며 경계하게 되내요. 제게도 문제가 있지만 약간은. 쩝.
^^;; 말이 그렇다는 거에요. 그럼 강좌 시작.
아참. 2000년 7월 4일자로 '쉬'편이 모두 다시 올라왔습니다. 그 이전에 갈무리 하신 분은 새로 갈무리하세요.
그리고 이 강좌에서 만드는 게시판은 제가 직접 테스트하며 소스를 코딩하는 것이기 때문에 대부분 에러 없이 작동됩니다. ^^; (사실 에러가 안날 정도로 단순한 게시판이지만 --;)
------------------------------------------------------------------------------------------------ '쉬'편은 모든 편이 서로 약간씩 관련이 되어 있으므로 모두를 한꺼번에 받으셔서 이 편 저 편 이동하시며 같이 읽는것이 좋습니다.^^;
이유가 각 부분마다 사용하는 변수가 많기 때문에 하나 하나 보시면 이 변수가 뭐에 쓰이는건지 헷갈릴 수 있거든요. 그래서 '쉬-짠1'편에서는 각 부분마다 사용되는 변수를 설명해놓고 있지만, 역시나 더 좋은 방법은 다른 편들을 이동하시며 보는 것입니다. :) '쉬'편의 짠들은 다음과 같으니 별도로 준비하셔서 참조하세요. ^^;
쉬-짠1 : 게시판 만들면서 사용되는 변수 정리 문서 쉬-짠2 : 게시판 만드는데 필요한 약간의 html tag 쉬-짠3 : 게시판 만들면서 만든 사용자 정의 함수 쉬-짠4 : 게시판 만드는데 사용된 php 함수 정리 쉬-짠5 : 게시판 만들면서 필요한 SQL 명령어 정리 ------------------------------------------------------------------------------------------------
쉬. 기능 붙이기
a. 글 수정 하기
글 삭제와 거의 동일합니다. 일단 글 수정 폼부터 볼까요?
<? /*-------------------------- filename : edit.php3 --------------------------*/ $connect = mysql_connect("localhost","아이디","비번"); mysql_select_db("사용DB명",$connect);
$result = mysql_query("SELECT * FROM $board WHERE no=$no", $connect);
$view = mysql_fetch_array($result);
echo " <form method="post" action="run.php3" enctype="multipart/form-data"> <input type="hidden" name="board" value="$board"> <input type="hidden" name="mode" value="modify"> <input type="hidden" name="modno" value="$no">
작성자명 : <input type="text" name="edt[name]" value="$view[usrname]"><br> E-mail : <input type="text" name="edt[email]" value="$view[usremail]"><br> Homepage : <input type="text" name="edt[homepage]" value="$view[usrhomepage]"><br> 글 번호 : $view[num]<br> 글 제목 : <input type="text" name="edt[title]" value="$view[usrtitle]"><br> 조회수 : $view[hit]<br> 본문 : <p> <textarea name="edt[contents]" cols="36" rows="10"> $view[contents] </textarea> <p> 비밀 번호 : <input type="password" name="modpw"> <input type="submit" value="수정"> </form> "; ?> |
으흠. 글 삭제 폼과 거의 동일하죠? 다른 점이 있다면 글 내용이 <input> tag 에서 value 로 들어가있다는 점입니다. :] 이정도까지는 굳이 설명 없어도 될 터이니 넘어갈께요. ^^;
이제 글을 수정해야 합니다. 글 입력자는 글 수정 폼에서 어떠한 정보를 수정할지는 알 수 없습니다. 사실 글 수정이라면 SQL 명령어에서 UPDATE 문을 써야겠지만 이용자가 어떤 항목을 수정할 지 모르니 UPDATE 문을 쓰기가 조금 난처합니다.
그래서 아예 글을 새로 저장할까 합니다. 어떻게 글을 새로 저장해야 할까요?
수정되는 글을 삭제하고 글 입력시 삭제된 글 번호로 글이 입력되면 되겠죠? 물론 조회수나 글 입력 시간은 이전의 글 데이터를 그대로 써야합니다. :]
구현 소스를 볼까요?
elseif ($mode == "modify") { $result = mysql_query(" SELECT * FROM $board WHERE no=$modno", $connect);
$oldarti = mysql_fetch_array($result);
$origi = substr($oldarti[usrpasswd], 0, 2); $inppw = crypt($modpw, $origi);
if ($oldarti[usrpasswd] != $inppw) { echo "비밀번호가 일치하지 않습니다."; exit; }
$result = mysql_query("DELETE FROM $board WHERE no=$modno", $connect);
$result = mysql_query("INSERT INTO $board VALUES( $oldarti[no], $oldarti[num], $oldarti[idx], '$edt[title]', '$edt[contents]', $oldarti[replyon], $oldarti[replyto], $oldarti[replyfrom], $oldarti[replydepth], $oldarti[hit], '$oldarti[filepath]', '$oldarti[filename]',$oldarti[filesize], '$edt[name]', '$inppw', '$edt[email]', '$edt[homepage]',$oldarti[filluptime])", $connect); } |
생각보다 복잡하죠? 글 수정 과정은 아래와 같습니다.
1. 수정할 글의 내용을 미리 읽어와 백업해둔다. 2. 삭제할 글을 삭제한다. 3. 수정시키는 내용을 제외한 모든 내용을 새로 입력시킨다. 단 글 번호는 수정할 글 번호로 한다. 여기서 수정시키는 내용은 '글 제목, 작성자명, email주소, 홈페이지 주소, 본문'이다.
에헤. 간단하죠? 1번 과정에 해당되는 것이
$result = mysql_query(" SELECT * FROM $board WHERE no=$modno", $connect);
$oldarti = mysql_fetch_array($result); |
입니다. 중간에 비밀 번호 체크 부분은 이미 설명해드렸으니 넘어가고. 2번 과정은
$result = mysql_query("DELETE FROM $board WHERE no=$modno", $connect); |
부분입니다. 해당 글을 삭제하죠? 그런 뒤 글을 새로 입력시키는 3 번 과정은
$result = mysql_query("INSERT INTO $board VALUES( $oldarti[no], $oldarti[num], $oldarti[idx], '$edt[title]', '$edt[contents]', $oldarti[replyon], $oldarti[replyto], $oldarti[replyfrom], $oldarti[replydepth], $oldarti[hit], '$oldarti[filepath]', '$oldarti[filename]',$oldarti[filesize], '$edt[name]', '$inppw', '$edt[email]', '$edt[homepage]',$oldarti[filluptime])", $connect); |
입니다. :]
이미 각 각은 글 입력 및 삭제 등을 통해 해왔던 것이기 때문에 굳이 별도의 설명은 필요 없겠죠? :]
자아. 그럼 내용이 추가된 run.php3 를 살펴 보겠습니다.
<? /*-------------------------- filename : run.php3 --------------------------*/ $connect = mysql_connect("localhost","아이디","비번"); mysql_select_db("사용DB명",$connect);
if ($mode == "write") { $result = mysql_query(" SELECT MAX(num) AS num, MAX(idx) AS idx, MAX(no) AS no FROM $board", $connect);
$ist[num] = mysql_result($result, 0, "num"); $ist[num] += 1; $ist[idx] = mysql_result($result, 0, "idx"); $ist[idx] += 1;
mysql_free_result($result);
$intime = time(); $passwd = crypt($fil[pw]);
$result = mysql_query("INSERT INTO $board VALUES('', $ist[num], $ist[idx], '$fil[title]', '$fil[text]', 0, 0, 0, 0, 0, '', '', 0, '$fil[name]', '$passwd', '$fil[email]', '$fil[homepage]', $intime)", $connect); } elseif ($mode == "remove") { $result = mysql_query(" SELECT * FROM $board WHERE no=$delno",$connect); $dels = mysql_fetch_array($result);
$origi = substr($dels[usrpasswd], 0, 2); $inppw = crypt($delpw, $origi);
if ($dels[usrpasswd] != $inppw) { echo "비밀번호가 일치하지 않습니다."; exit; }
$result = mysql_query("DELETE FROM $board WHERE no=$delno", $connect); } elseif ($mode == "modify") { $result = mysql_query(" SELECT * FROM $board WHERE no=$modno", $connect);
$oldarti = mysql_fetch_array($result);
$origi = substr($oldarti[usrpasswd], 0, 2); $inppw = crypt($modpw, $origi);
if ($oldarti[usrpasswd] != $inppw) { echo "비밀번호가 일치하지 않습니다."; exit; }
$result = mysql_query("DELETE FROM $board WHERE no=$modno", $connect);
$result = mysql_query("INSERT INTO $board VALUES( $oldarti[no], $oldarti[num], $oldarti[idx], '$edt[title]', '$edt[contents]', $oldarti[replyon], $oldarti[replyto], $oldarti[replyfrom], $oldarti[replydepth], $oldarti[hit], '$oldarti[filepath]', '$oldarti[filename]',$oldarti[filesize], '$edt[name]', '$inppw', '$edt[email]', '$edt[homepage]',$oldarti[filluptime])", $connect); } ?> |
냐항항. 이미 설명했던 것을 재활용하는지라 이번 회는 매우 손쉽게 넘어가버렸군요. ^^; 잘 모르겠다 싶으시면 다른 편을 살펴보세요. 이미 말씀드렸듯이 '쉬'편은 각 편들을 서로 살펴봐야 된답니다. ^^;
이제 이렇게 완성된 게시판을 좀 더 이쁘고 에러 처리로 깔끔하게 만들어보겠습니다. ^^; 물론 게시판으로서의 기능은 현재로도 충분하긴 하지만요. :]
다음 강좌에서 뵈요.
---------------------------- 함께하면 즐거운 사이트들 (-_-; 광고임 -_-;) http://game.creple.com/delthia http://creple.com http://coco.st ----------------------------
- ?! 디망쉬
|