PHP 프로그래밍

본문 바로가기
사이트 내 전체검색


Web Programming >> PHP Programming
[목차]
제13장 자료실 프로그램(MySQL)

    1. MySQL을 이용한 자료실 만들기

 자료실은 게시판의 처리와 거의 유사합니다. 단지 파일 업로드와 관련된 부분이 추가되는 것 뿐입니다. 그러므로, 이전장에서 만들 게시판을 이용하여 작업합니다. 검색기능 페이지 이동 기능도 게시판에서 설명하였으므로 생략합니다.

지금부터 따라가면서 만들어보기 바랍니다.

 

1. 자료실 테이블 만들기

    create table pds (
    uid       int not null default '0' auto_increment,
    num    int  not null default '0',
    writer   varchar(20),
    email   varchar(60),
    subject    varchar(50),
    content    blob,
    passwd    varchar(30),
    r_step      varchar(250),
    regtime    datetime,
    visit         int,
    up_file    varchar(200),
    primary key(uid));

 

 

2. 자료실의 기능들...

  • 목록 출력(list.html)
  • 새로운 자료 등록하기(writeform.html, write.html)
  • 내용보기(read.html)
  • 답변글 쓰기(replyform.html, reply.html)
  • 수정하기(modifyform.html, modify.html)
  • 삭제하기(deleteform.html, delete.html)

 

<그림> 자료실 흐름도

 

 

3. 목록출력하기(list.html)

 

<?

if(!$db) $db = "pds";

 

########### 한 페이지당 출력할 게시물의 수

$num_per_page = 5;

 

########### 게시물 출력목록 하단에 링크를 걸 페이지의 개수

$page_per_block = 5;

 

########### 페이지 초기화

if(!$page) {

     $page = 1;

}

 

########## 데이터베이스 서버에 연결한다. ##########

$dbconn = mysql_connect("localhost","php","php99") or die("DB서버 연결에 실패하였습니다!");

 

########## 작업 데이터베이스로 sample을 선택한다. ##########

$status = mysql_select_db("sample",$dbconn);

if(!$status) {

   $errNO = mysql_errno($dbconn);

   $errMSG = mysql_error($dbconn);

 

   echo("서버의 sample 데이터베이스연결에 실패하였습니다!<br>");

   echo("에러코드 $errNO : $errMSG<br>");

   exit;

}

 

$query = "SELECT num,subject,writer,visit,regtime,r_step FROM $db ";

if($keyword) $query .= " where $type like '%$keyword%'";

$query .= " ORDER BY num DESC,r_step ASC";

 

$result = mysql_query($query,$dbconn);

if(!$result) {

   $errNO = mysql_errno($dbconn);

   $errMSG = mysql_error($dbconn);

       

   echo("에러코드 $errNO : $errMSG<br>");

   exit;

}

 

$total_record = mysql_num_rows($result);

 

########## 전체 페이지수를 계산한다. ##########

$total_page = ceil($total_record/$num_per_page);

 

########## 현재의 페이지에서 출력할 게시물의 범위를 결정한다. ##########

if(!$total_record) {

   $first = 1;

   $last = 0;   

} else {

   $first = $num_per_page*($page-1);

   $last = $num_per_page*$page;

 

   $last -= 1;

   if($total_page == $page) {

      $last = $total_record - 1;

   }

}

 

echo "

<HEAD>

<TITLE>LeeLAB - 자료실</TITLE>

<STYLE TYPE='text/css'>

<!--

A:link{text-decoration:none;color:}

A:visited{text-decoration:none;color:}

A:hover {background-color:dddddd}

-->

</STYLE>

</HEAD>

<BODY BGCOLOR=eeeeee>

<CENTER>

<FONT SIZE=6 FACE='Comic Sans MS'>[ 자료실 ]</FONT><BR>

 

<TABLE BORDER=0 WIDTH=600 CELLSPACING=1>

<TR>

        <TD ALIGN=LEFT><FONT SIZE=2>총 게시물 : $total_record</TD>

        <TD ALIGN=RIGHT><FONT SIZE=2>현재 페이지 : $page / $total_page

</TD>

</TR>

</TABLE>

<TABLE BORDER=0 WIDTH=600 CELLSPACING=1 BGCOLOR='black'>

<TR>

        <TD ALIGN=CENTER BGCOLOR=bbbbbb WIDTH=30><FONT SIZE=2 COLOR=black>번호</TD>

        <TD ALIGN=CENTER BGCOLOR=bbbbbb WIDTH=350><FONT SIZE=2 COLOR=black>제목</TD>

        <TD ALIGN=CENTER BGCOLOR=bbbbbb WIDTH=110><FONT SIZE=2 COLOR=black>작성자</TD>

        <TD ALIGN=CENTER BGCOLOR=bbbbbb WIDTH=30><FONT SIZE=2 COLOR=black>조회</TD>

        <TD ALIGN=CENTER BGCOLOR=bbbbbb WIDTH=80><FONT SIZE=2 COLOR=black>작성시간</TD>

</TR>";

########## 테이블내 모든 레코드의 각 데이터 값을 각각의 변수에 저장하여 출력한다.

for($i = $first; $i <= $last; $i++) {

        $num = mysql_result($result,$i,0);

        $subject = mysql_result($result,$i,1);

        $writer = mysql_result($result,$i,2);

        $visit = mysql_result($result,$i,3);

        $regtime = mysql_result($result,$i,4);

        $r_step = mysql_result($result,$i,5);

 

        $step_len = strlen($r_step) -1;

 

        if($keyword) ${$type} = eregi_replace("$keyword", "<font color=red>${keyword}</font>", ${$type});

 

        $regtime = substr($regtime,0,10);

        echo "

<TR>

        <TD ALIGN=CENTER BGCOLOR='eeeeee'><FONT SIZE=2>";

        if($r_step == "A") echo $num;

        else echo "&nbsp;";

        echo "</FONT></TD>

        <TD BGCOLOR='eeeeee'><FONT SIZE=2>";

        for($j=0; $j<$step_len; $j++) echo "&nbsp;&nbsp;";

        echo "&nbsp;<A HREF='read.html?db=$db&num=$num&page=$page&r_step=$r_step'>$subject</A></FONT></TD>

        <TD ALIGN=CENTER BGCOLOR='eeeeee'><FONT SIZE=2>$writer</FONT></TD>

        <TD ALIGN=CENTER BGCOLOR='eeeeee'><FONT SIZE=2>$visit</FONT></TD>

        <TD ALIGN=CENTER BGCOLOR='eeeeee'><FONT SIZE=2>$regtime</FONT></TD>

</TR>";

}

?>

</TABLE><BR>

<table width="600" border="0" cellspacing="0" cellpadding="0" align="center">

<tr>

   <td colspan="6" align="center">

<?

### 게시물 목록 하단의 각 페이지로 직접 이동할 수 있는

### 페이지링크에 대한 설정을 한다.

$total_block = ceil($total_page/$page_per_block);

$block = ceil($page/$page_per_block);

 

$first_page = ($block-1)*$page_per_block;

$last_page = $block*$page_per_block;

 

if($total_block <= $block) {

   $last_page = $total_page;

}

 

########## 이전페이지블록에 대한 페이지 링크 ##########

if($block > 1) {

   $my_page = $first_page;

   echo("<font size=2><a href=\"$php_self?page=$my_page\" >

   [이전 ${page_per_block}개]</a></font>");

}

 

### 현재의 페이지 블럭범위내에서 각 페이지로 바로

### 이동할 수 있는 하이퍼링크를 출력한다.

for($direct_page = $first_page+1; $direct_page <= $last_page; $direct_page++) {

   if($page == $direct_page) {

      echo("<font size=2><b>[$direct_page]</b></font>");

   } else {

      echo("<font size=2><a href=\"list.html?page=$direct_page\">

      [$direct_page]</a></font>");

   }

}

 

########## 다음페이지블록에 대한 페이지 링크 ##########

if($block < $total_block) {

   $my_page = $last_page+1;

   echo("<font size=2><a href=\"list.html?page=$my_page\">

   [다음 ${page_per_block}개]</a></font>");

}

?>   

   </td>

</tr>

</table>

<?

echo "

<TABLE WIDTH=600 CELLSPACING=1 CELLPADDING=3>

<TR>

        <TD ALIGN=LEFT><FONT SIZE=2>

        [ ";

        $ppage = $page + 1;

        $npage = $page - 1;

 

        if($page > 1) echo "<A HREF='list.html?db=$db&page=$ppage'>이전페이지</A>";

        else echo "이전페이지";

        echo "]&nbsp;&nbsp;

        [ <A HREF='writeform.html?db=$db&page=$page'>글쓰기</A> ]&nbsp;&nbsp;

        [ ";

        if($page < $total_page) echo "<A HREF='list.html?db=$db&page=$npage'>다음페이지</A>";

        else echo "다음페이지";

        echo "]</TD>

<FORM METHOD=POST ACTION=list.html?db=$db>

        <TD ALIGN=RIGHT><SELECT NAME=type>

        <OPTION VALUE='subject'>제목</OPTION>

        <OPTION VALUE='writer'>글쓴이</OPTION>

        <OPTION VALUE='content'>내용</OPTION></SELECT>&nbsp;

        <INPUT TYPE=TEXT NAME=keyword SIZE=10>&nbsp;

        <INPUT TYPE=SUBMIT VALUE='찾기'></TD>

        </FORM>

        </TR>

</TABLE>";

 

?>

 

 

 

4. 새로운 게시물 등록하기(writeform.html, write.html)

 

등록 폼 만들기(writeform.html)

<HEAD>

<TITLE>LeeLAB - 자료실</TITLE>

<STYLE TYPE='text/css'>

<!--

A:link{text-decoration:none;color:}

A:visited{text-decoration:none;color:}

A:hover {background-color:dddddd}

-->

</STYLE>

 

<SCRIPT LANGUAGE='javascript'>

function chk_form()

{

        if(document.signform.writer.value == '') alert('이름을 입력하여 주세요.');

        else if(document.signform.subject.value == '') alert('제목을 입력하여 주세요');

        else if(document.signform.content.value.length < 10) alert('내용을 성심껏 입력해주세요');

        else if(document.signform.passwd.value == '') alert('비밀번호는 추후 글을 수정,삭제하는데 필요하니 입력해주세요.');

        else document.signform.submit();

}

</SCRIPT>

</HEAD>

<BODY BGCOLOR='eeeeee'>

<CENTER>

<FONT SIZE=6 FACE='Comic Sans MS'>[ 자료실 ]</FONT><BR>

<FORM NAME='signform' METHOD=POST ACTION='write.html'  ENCTYPE='multipart/form-data'>

<TABLE WIDTH=600 BGCOLOR=black CELLSPACING=1>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 쓴 이</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='writer' SIZE=12 MAXLENGTH=12></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>E-Mail</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='email' SIZE=50 MAXLENGTH=100></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 제 목</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='subject' SIZE=50 MAXLENGTH=100></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 내 용</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><TEXTAREA NAME='content' ROWS=10 COLS=67></TEXTAREA></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>비밀번호</TD>

        <TD BGCOLOR='eeeeee'><INPUT TYPE=PASSWORD NAME='passwd' SIZE=12 MAXLENGTH=12>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100>

        <FONT SIZE=2 COLOR='black'>첨부파일</TD>

        <TD BGCOLOR='eeeeee'>

        <INPUT TYPE=FILE NAME=up_file></TD>

</TR>

 

</TABLE>

<TABLE WIDTH=600 BGCOLOR=eeeeee>

<TR>

        <TD ALIGN=CENTER>

        <INPUT TYPE='BUTTON' VALUE='저장' onclick='chk_form();'>&nbsp;&nbsp;

        <INPUT TYPE='BUTTON' VALUE='취소' onclick='javascript:location = "<?echo("list.html?db=$db&page=$page");?>"'></TD>

</TR>

</TABLE>

<INPUT TYPE=HIDDEN NAME=db VALUE='<?echo $db;?>'>

</FORM>

</BODY>

</HTML>

 

 

등록 처리 프로그램(write.html)

<?

$connect = mysql_connect("localhost","php","php99");

mysql_select_db("sample",$connect);

 

if($up_file_name)

{

        $file_path = "./data/".$up_file_name;

        copy($up_file, $file_path) or die("파일 업로드에 실패 하였습니다.");

        unlink($up_file);

}

//echo($up_file_name);

 

$writer = addslashes($writer);

$email = addslashes($email);

$subject = addslashes($subject);

$content = addslashes($content);

$passwd = addslashes($passwd);

 

$query = "SELECT max(num) FROM $db";

$result = mysql_query($query,$connect);

$num = mysql_result($result,0,0)+1;

 

$query = "INSERT INTO $db (num,writer,email,subject,content,passwd,r_step,regtime,visit,up_file) VALUES ($num,'$writer','$email','$subject','$content','$passwd','A',NOW(),0,'$up_file_name')";

$result = mysql_query($query,$connect);

echo "<SCRIPT>location.replace('list.html?db=$db&page=$page');</SCRIPT>";

 

?>

 

 

 

5. 게시물 내용 보기(read.html)

 

<?

$connect = mysql_connect("localhost","php","php99");

mysql_select_db("sample",$connect);

 

$query = "UPDATE $db SET visit=visit+1 WHERE num=$num AND r_step='$r_step'";

$result = mysql_query($query,$connect);

$query = "SELECT * FROM $db WHERE num=$num AND r_step='$r_step'";

$result = mysql_query($query,$connect);

$row = mysql_fetch_array($result);

 

$writer = stripslashes($row[writer]);

$email = stripslashes($row[email]);

$subject = stripslashes($row[subject]);

$content = stripslashes($row[content]);

$passwd = stripslashes($row[passwd]);

 

$content = nl2br($content);

echo "

<HEAD>

<TITLE>LeeLAB - 자료실</TITLE>

<STYLE TYPE='text/css'>

<!--

A:link{text-decoration:none;color:}

A:visited{text-decoration:none;color:}

A:hover {background-color:dddddd}

-->

</STYLE>

 

</HEAD>

<BODY BGCOLOR=eeeeee LINK=black ALINK=black VLINK=black>

<CENTER>

<FONT SIZE=6 FACE='Comic Sans MS'>[ 자료실 ]</FONT><BR>

 

<TABLE WIDTH=600 CELLSPACING=1 CELLPADDING=3 BGCOLOR=black>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=80><FONT SIZE=2 COLOR=black>작성자</FONT></TD>

        <TD BGCOLOR='eeeeee' WIDTH=280><FONT SIZE=2>&nbsp;$writer</TD>

        <TD BGCOLOR='bbbbbb'

        ALIGN=CENTER WIDTH=80><FONT SIZE=2 COLOR=black>작성시간</FONT></TD>

        <TD BGCOLOR='eeeeee' WIDTH=160 ALIGN=CENTER><FONT SIZE=2>$row[regtime]</TD>

</TR>

<TR>

 

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=80><FONT SIZE=2 COLOR=black>E-Mail</FONT></TD>

        <TD BGCOLOR='eeeeee' WIDTH=280>

        <FONT SIZE=2><A HREF='mailto:$row[email]'>$row[email]</A></TD>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=80><FONT SIZE=2 COLOR=black>조회수</FONT></TD>

        <TD BGCOLOR='eeeeee' WIDTH=160 ALIGN=CENTER><FONT SIZE=2>$row[visit]</TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=bbbbbb>

        <FONT SIZE=2 COLOR=black>제목</FONT></TD>

        <TD BGCOLOR='eeeeee'><FONT SIZE=2>&nbsp;$subject</TD>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=bbbbbb>

        <FONT SIZE=2 COLOR=black>첨부파일</FONT></TD>

        <TD BGCOLOR='eeeeee'><FONT SIZE=2>&nbsp;

        <A HREF='./data/$row[up_file]'>".$row[up_file]."</A></TD>

</TR>

</TABLE>

<BR>

<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=1 BGCOLOR='black' WIDTH=600>

<TR>

        <TD BGCOLOR='eeeeee' COLSPAN=2><BR><FONT SIZE=2>$content<BR><BR></TD>

</TR>

</TABLE>

<BR>

<TABLE WIDTH=600 CELLSPACING=1 CELLPADDING=3>

<TR>

        <TD ALIGN=CENTER><FONT SIZE=2>

        [ <A HREF='list.html?db=$db&page=$page'>글목록</A> ]&nbsp;&nbsp;

        [ <A HREF='modifyform.html?db=$db&num=$num&r_step=$r_step&page=$page'>글수정</A> ]&nbsp;&nbsp;

        [ <A HREF='replyform.html?db=$db&num=$num&r_step=$r_step&page=$page'>답변글</A> ]&nbsp;&nbsp;

        [ <A HREF='deleteform.html?db=$db&num=$num&r_step=$r_step&page=$page'>삭제하기</A> ]</TD>

</TR>

</TABLE>";

?>

 

 

6. 답변글 쓰기(replyform.html, reply.html)

 

 

답변글 쓰기 폼 만들기(replyform.html)

 <?

$connect = mysql_connect("localhost","php","php99");

mysql_select_db("sample",$connect);

 

$query = "SELECT content FROM $db WHERE num=$num AND r_step='$r_step'";

$result = mysql_query($query,$connect);

$row = mysql_fetch_array($result);

 

$writer = stripslashes($row[writer]);

$email = stripslashes($row[email]);

$subject = stripslashes($row[subject]);

$content = stripslashes($row[content]);

$passwd = stripslashes($row[passwd]);

 

?>

<HEAD>

<TITLE>LeeLAB - 자료실</TITLE>

<STYLE TYPE='text/css'>

<!--

A:link{text-decoration:none;color:}

A:visited{text-decoration:none;color:}

A:hover {background-color:dddddd}

-->

</STYLE>

 

<SCRIPT LANGUAGE='javascript'>

function chk_form()

{

        if(document.replyform.writer.value == '') alert('이름을 입력하여 주세요.');

        else if(document.replyform.subject.value == '') alert('제목을 입력하여 주세요');

        else if(document.replyform.content.value.length < 10) alert('내용을 성심껏 입력해주세요');

        else if(document.replyform.passwd.value == '') alert('비밀번호는 추후 글을 수정,삭제하는데 필요하니 입력해주세요.');

        else document.replyform.submit();

}

</SCRIPT>

</HEAD>

<BODY BGCOLOR='eeeeee'>

<CENTER>

<FONT SIZE=6 FACE='Comic Sans MS'>[ 자료실 ]</FONT><BR>

<FORM NAME='replyform' METHOD=POST ACTION='reply.html'>

<TABLE WIDTH=600 BGCOLOR=black CELLSPACING=1>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 쓴 이</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='writer' SIZE=12 MAXLENGTH=12></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>E-Mail</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='email' SIZE=50 MAXLENGTH=100></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 제 목</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='subject' SIZE=50 MAXLENGTH=100 VALUE='☞ <?echo "$row[subject]";?>'></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 내 용</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><TEXTAREA NAME='content' ROWS=10 COLS=67>

        <?echo "답변 : $row[content]

------------------------------------------------------------\n";?>

        </TEXTAREA></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>비밀번호</TD>

        <TD BGCOLOR='eeeeee'><INPUT TYPE=PASSWORD NAME='passwd' SIZE=12 MAXLENGTH=12>

</TR>

</TABLE>

<TABLE WIDTH=600 BGCOLOR=eeeeee>

<TR>

        <TD ALIGN=CENTER>

        <INPUT TYPE='BUTTON' VALUE='저장' onclick='chk_form();'>&nbsp;&nbsp;

        <INPUT TYPE='BUTTON' VALUE='취소' onclick='javascript:location = "<?echo("list.html?db=$db&page=$page");?>"'></TD>

</TR>

</TABLE>

<INPUT TYPE=HIDDEN NAME=db VALUE='<?echo $db;?>'>

<INPUT TYPE=HIDDEN NAME=num VALUE='<?echo $num;?>'>

<INPUT TYPE=HIDDEN NAME=page VALUE='<?echo $page;?>'>

</FORM>

</BODY>

</HTML>

 

 

답변글 쓰기 처리 프로그램(reply.html)

<?

$connect = mysql_connect("localhost","php","php99");

mysql_select_db("sample",$connect);

 

$writer = addslashes($writer);

$email = addslashes($email);

$subject = addslashes($subject);

$content = addslashes($content);

$passwd = addslashes($passwd);

 

$query = "SELECT r_step FROM $db WHERE num=$num AND length(r_step)=length('$r_step')+1 ORDER BY r_step ";

$result = mysql_query($query,$connect);

 

$rows = mysql_num_rows($result);

if($rows) {

        $row = mysql_fetch_array($result);

 

        $step = substr($row[r_step],strlen($r_step)-1,1);

        $step = ++$step;

 

        $r_step = $r_step . $step;

 

} else {

        $r_step = $r_step . "A";

}

 

$query = "INSERT INTO $db (num,writer,email,subject,content,passwd,r_step,regtime,visit) VALUES ($num,'$writer',  '$email','$subject','$content','$passwd','$r_step',NOW(),0)";

$result = mysql_query($query,$connect);

if(!$result) {

        echo($query);

        exit;

}

echo "<SCRIPT>location.replace('list.html?db=$db&page=$page');</SCRIPT>";

 

?>

 

 

 

 

7. 게시물 수정하기(modifyform.html, modify.html)

 

게시물 수정 폼 만들기(modifyform.html)

 <?

$connect = mysql_connect("localhost","php","php99");

mysql_select_db("sample",$connect);

 

if(!$r_step) $r_step=0;

$query = "SELECT * FROM $db WHERE num=$num AND r_step='$r_step'";

$result = mysql_query($query,$connect);

$row = mysql_fetch_array($result);

 

$writer = stripslashes($row[writer]);

$email = stripslashes($row[email]);

$subject = stripslashes($row[subject]);

$content = stripslashes($row[content]);

$passwd = stripslashes($row[passwd]);

 

?>

<HEAD>

<TITLE>LeeLAB - 자료실</TITLE>

<STYLE TYPE='text/css'>

<!--

A:link{text-decoration:none;color:}

A:visited{text-decoration:none;color:}

A:hover {background-color:dddddd}

-->

</STYLE>

<SCRIPT LANGUAGE='javascript'>

function chk_form()

{

        if(document.modifyform.writer.value == '') alert('이름을 입력하여 주세요.');

        else if(document.modifyform.subject.value == '') alert('제목을 입력하여 주세요');

        else if(document.modifyform.content.value.length < 10) alert('내용을 성심껏 입력해주세요');

        else if(document.modifyform.passwd.value == '') alert('글을 수정하시려면 비밀번호가 필요합니다..');

        else document.modifyform.submit();

}

</SCRIPT>

</HEAD>

<BODY BGCOLOR='eeeeee'>

<CENTER>

<FONT SIZE=6 FACE='Comic Sans MS'>[ 자료실 ]</FONT><BR>

 

<FORM NAME='modifyform' METHOD=POST ACTION='modify.html'>

<TABLE WIDTH=600 BGCOLOR=black CELLSPACING=1>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 쓴 이</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='writer' SIZE=12 MAXLENGTH=12 VALUE='<?echo $writer;?>'></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>E-Mail</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='email' SIZE=50 MAXLENGTH=100 VALUE='<?echo $email;?>'></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 제 목</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><INPUT TYPE=TEXT NAME='subject' SIZE=50 MAXLENGTH=100 VALUE='<?echo $subject;?>'></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>글 내 용</TD>

        <TD BGCOLOR='eeeeee' WIDTH=500><TEXTAREA NAME='content' ROWS=10 COLS=67><?echo $content;?></TEXTAREA></TD>

</TR>

<TR>

        <TD BGCOLOR='bbbbbb' ALIGN=CENTER WIDTH=100><FONT SIZE=2 COLOR='black'>비밀번호</TD>

        <TD BGCOLOR='eeeeee'><INPUT TYPE=PASSWORD NAME='passwd' SIZE=12 MAXLENGTH=12>

</TR>

</TABLE>

<TABLE WIDTH=600 BGCOLOR=eeeeee>

<TR>

        <TD ALIGN=CENTER>

        <INPUT TYPE='BUTTON' VALUE='저장' onclick='chk_form();'>&nbsp;&nbsp;

        <INPUT TYPE='BUTTON' VALUE='취소' onclick='javascript:location = "<?echo("list.html?db=$db&page=$page");?>"'></TD>

</TR>

</TABLE>

<INPUT TYPE=HIDDEN NAME=db VALUE='<?echo $db;?>'>

<INPUT TYPE=HIDDEN NAME=num VALUE='<?echo $num;?>'>

<INPUT TYPE=HIDDEN NAME=r_step VALUE='<?echo $r_step;?>'>

<INPUT TYPE=HIDDEN NAME=page VALUE='<?echo $page;?>'>

 

</FORM>

</BODY>

</HTML>

 

 

게시물 수정 처리 프로그램(modify.html)

 <?

$connect = mysql_connect("localhost","php","php99");

mysql_select_db("sample",$connect);

 

$writer = addslashes($writer);

$email = addslashes($email);

$subject = addslashes($subject);

$content = addslashes($content);

$passwd = addslashes($passwd);

 

$query = "SELECT passwd FROM $db WHERE num=$num AND r_step='$r_step'";

$result = mysql_query($query,$connect);

$row = mysql_fetch_array($result);

 

if($row[passwd] == $passwd) {

        $query = "UPDATE $db SET writer='$writer',email='$email',subject='$subject',content='$content' WHERE num=$num AND r_step='$r_step'";

        $result = mysql_query($query,$connect);

        echo "<SCRIPT>location.replace('read.html?db=$db&num=$num&r_step=$r_step&page=$page');</SCRIPT>";

}

else {

        echo "<SCRIPT>alert('비밀번호가 틀렸습니다.');history.go(-1);</SCRIPT>";

        exit;

}

?>

 

 

8. 게시물 삭제하기(deleteform.html, delete.html)

 

게시물 삭제 폼 만들기(deleteform.html)

 <HEAD>

<TITLE>LeeLAB - 자료실</TITLE>

<STYLE TYPE='text/css'>

<!--

A:link{text-decoration:none;color:}

A:visited{text-decoration:none;color:}

A:hover {background-color:dddddd}

-->

</STYLE>

</HEAD>

<BODY>

<CENTER>

<FONT SIZE=6 FACE='Comic Sans MS'>[ 자료실 ]</FONT><BR>

 

<FORM METHOD=POST ACTION=delete.html>

<TABLE WIDTH=300 CELLSPACING=1 CELLPADDING=3 BGCOLOR=black>

<TR>

        <TD ALIGN=CENTER BGCOLOR=dddddd><FONT SIZE=2>게시물 암호</FONT></TD>

</TR>

<TR>

        <TD ALIGN=CENTER BGCOLOR=dddddd><INPUT TYPE=PASSWORD NAME=passwd SIZE=10></TD>

</TR>

<TR>

        <TD ALIGN=CENTER BGCOLOR=dddddd><INPUT TYPE=SUBMIT VALUE='삭제하기'>

        <INPUT TYPE='BUTTON' VALUE='취소' onclick='javascript:location = "<?echo("list.html?db=$db&page=$page");?>"'></TD>

</TR>

</TABLE>

<INPUT TYPE=HIDDEN NAME=db VALUE='<?echo $db;?>'>

<INPUT TYPE=HIDDEN NAME=num VALUE='<?echo $num;?>'>

<INPUT TYPE=HIDDEN NAME=r_step VALUE='<?echo $r_step;?>'>

<INPUT TYPE=HIDDEN NAME=page VALUE='<?echo $page;?>'>

</FORM>

</BODY>

</HTML>

 

 

 

게시물 삭제 처리 프로그램(delete.html)

<?

$connect = mysql_connect("localhost","php","php99");

mysql_select_db("sample",$connect);

 

$writer = addslashes($writer);

$email = addslashes($email);

$subject = addslashes($subject);

$content = addslashes($content);

$passwd = addslashes($passwd);

 

$query = "SELECT passwd,up_file FROM $db WHERE num=$num AND r_step='$r_step'";

$result = mysql_query($query,$connect);

$row = mysql_fetch_array($result);

 

if($row[passwd] == $passwd) {

        $query = "DELETE FROM $db WHERE num=$num AND r_step='$r_step'";

        $result = mysql_query($query,$connect);

 

        if($row[up_file]) unlink("./data/$row[up_file]");

 

        echo "<SCRIPT>location.replace('list.html?db=$db&page=$page');</SCRIPT>";

}

else {

        echo "<SCRIPT>alert('비밀번호가 틀렸습니다.');history.go(-1);</SCRIPT>";

        exit;

}

?>

 

[목차]

개인정보취급방침 서비스이용약관 모바일 버전으로 보기 상단으로

TEL. 063-469-4551 FAX. 063-469-4560 전북 군산시 대학로 558
군산대학교 컴퓨터정보공학과

Copyright © www.leelab.co.kr. All rights reserved.