PHP 프로그래밍

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


Web Programming >> PHP Programming
[목차]
제18장 BBS용 쪽지함 만들기(MySQL)

    1. BBS용 쪽지함 만들기

 

1. 쪽지함 테이블(myhome) 만들기

 

CREATE TABLE messages (

   uid int(10) unsigned NOT NULL auto_increment,

   from_id varchar(20) NOT NULL,

   to_id varchar(20) NOT NULL,

   signdate int(9) unsigned NOT NULL,

   message varchar(200),

   flag int(2) unsigned NOT NULL,

   PRIMARY KEY (uid)

);

 

 

2. 쪽지함 만들기

 

디렉토리구조는 /bbs/message/로서, 이 디렉토리 밑에 아래의 파일을 만들면 됩니다.

또한, BBS메뉴(/bbs/menu.html)에 쪽지함을 연결하면 됩니다.

파일명 : /bbs/menu.html

<?

if(!$user_id || !$user_name) {

        echo "<SCRIPT>location.replace('login.html');</SCRIPT>";

        exit;

}

?>

<HTML>

<HEAD>

<TITLE> 회원만이 볼 수 있는 화면 </TITLE>

</HEAD>

 

<BODY BGCOLOR="#FFFFFF">

<UL>

        <LI> <A HREF="passwd.html" target="leelab-detail">암호변경</A>

        <LI> <A HREF="change.html" target="leelab-detail">개인정보변경</A><br>

        <LI> <A HREF="admin/member" target="leelab-detail">회원관리</A><br>

 

        <LI> <A HREF="message/" target="_blank">쪽지함</A><br>

        <LI> <a href="logout.html" target="_top">종료</a>

</UL>

</BODY>

</HTML>

 

 

 

여기부터는 /bbs/message/ 디렉토리 밑에 파일을 만들면 됩니다.

파일명 : index.html

<HTML>

<HEAD>

<TITLE> Lee Lab - 쪽지함 보기 </TITLE>

</HEAD>

 

<frameset cols="150,*" BORDER=0 framespacing=0 frameborder=0>

<frame name="leelab-message-menu" src="menu.html" scrolling="no" marginwidth="0" marginheight="0">

<frame name="leelab-message-detail" src="list.html" scrolling="auto" marginwidth="0" marginheight="0">

</frameset>

</HTML>

 

 

파일명 : menu.html (메뉴판)

<html>

<head>

<title>쪽지함보기</title>

<META http-equiv=content-type content=text/html; charset=euc-kr>

</head>

 

<body bgcolor="#FFFFFF" leftmargin="0" rightmargin="0" marginwidth="0" oncontextmenu="return false" marginheight="0">

<br>

<table cellspacing=1 cellpadding=0 width='150' border=0 align=center bgcolor=black>

  <tr>

    <td align=center bgcolor="#BBBBBB" height="20"><font size='2'><b><font color=#3a1563>쪽지함</font><font

                  color=#88b200></font></b></font>

        </td>

  </tr>

  <tr>

        <td align=right bgcolor="#EEEEEE" height="20"><a href="member_search.html" target="leelab-message-detail"><font size='2'>회원찾기</a>

        </td>

  </tr>

  <tr>

        <td align=right bgcolor="#EEEEEE" height="20"><a href="message.html" target="leelab-message-detail"><font size='2'>쪽지보내기</a>

        </td>

  </tr>

  <tr>

        <td align=right bgcolor="#EEEEEE" height="20"><a href="delete.html" target="leelab-message-detail"><font size=2 color=green>쪽지함 비우기</font> </a>

        </td>

  </tr>

</table>

 

</body>

</html>

 

 

파일명 : list.html (쪽지함 보기)

<html>

<head>

<title>쪽지함보기</title>

<META http-equiv=content-type content=text/html; charset=euc-kr>

</head>

 

<body bgcolor="#FFFFFF" leftmargin="15" rightmargin="0" marginwidth="0" oncontextmenu="return false" marginheight="0">

<?

if(!$user_id) {

        echo "

                <SCRIPT>

                alert('로그인한 후에 이용하세요.');

                history.go(-1);

                </SCRIPT>";

        exit;

}

 

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

$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;

}

 

$result = mysql_query("select from_id,signdate,message,flag,uid from messages where to_id='$user_id' order by signdate DESC");

if(!$result) {

        echo "

                <SCRIPT>

                alert('QUERY ERROR');

                history.go(-1);

                </SCRIPT>";

        exit;

}

 

$total = mysql_num_rows($result);

              

if (!$total)

{

echo ("

 

<br><font color='black' size='2' face=돋움>새로온 쪽지가 없습니다.</font><br>

 

 ");

} else {

$counter = 0;

 

?>

<br>

<font size=2 color=red>읽으신 쪽지는 꼭 지워주세요! </font>

 

<?

while ($counter < $total):

   

$from_id = mysql_result($result, $counter, 0);

$signdate = mysql_result($result, $counter, 1);

$message = mysql_result($result, $counter, 2);

$flag = mysql_result($result, $counter, 3);

$my_uid = mysql_result($result, $counter, 4);

 

$message = stripslashes($message);

$message =  nl2br($message);

 

$result1 = mysql_query("select name from member where id='$from_id' ");

$row1 = mysql_num_rows($result1);

if($row1){

        $name = mysql_result($result1, 0, 0);

}

$signdate = date("Y-m-d H:i", $signdate);

 

if($flag) {

        $flag = "#eeeeee";

}

else {

        $flag = "#88bb88";

 

                $query1 = "UPDATE messages set flag = 1 where uid=$my_uid ";

                $result1 = mysql_query($query1);

                if (!$result1) {

                        echo "

                        <SCRIPT>

                        alert('QUERY ERROR');

                        history.go(-1);

                        </SCRIPT>";

                   exit;

                }

 

 

}

 

echo("

<table cellspacing=0 cellpadding=0 width='400' border=0>

  <tr>

    <td valign=center width='100%' bgcolor=#808080 height=1>

      <p align=center><img height=1 width=1></p>

    </td>

  </tr>

  <tr>

    <td valign=center width='100%' bgcolor= $flag>

      <table width='95%' border='0' align='center' cellpadding='3'>

        <tr>

          <td>

                        <font size='2'><a href='message.html?to_id=$id'>$name($from_id)</a></font> /

                        <font size='2' color='#FF3333'>$signdate</font>                 

                  </td>

        </tr>

        <tr>

          <td>

                          <font size='2' color='#0000CC'>$message</font>

                  </td>

        </tr>

      </table>

    </td>

  </tr>

  <tr>

    <td valign=center width='100%' bgcolor=#eeeeee height=10><img height=1   width=1></td>

  </tr>

</table>

");

 

$counter = $counter +1;

endwhile;

 

 

}

 

?>

 

</body>

</html>

 

 

파일명 : member_search.html (내친구 찾기)

 

<?

if(!$user_id) {

        echo "

                <SCRIPT>

                alert('로그인한 후에 이용하세요.');

                history.go(-1);

                </SCRIPT>";

        exit;

}

 

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

$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;

}

 

?>

<html>

<head>

<title>내 친구 찾기</title>

<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">

</head>

<body bgcolor='#ffffff'>

<br>

<?

if(strcmp($task,"process")) {

?>

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

              <tr>

                <td><font size="2"><b><font color=#3a1563>내 친구 </font><font

                  color=#88b200>찾기 </font></b></font></td>

                <td> &nbsp;

                </td>

              </tr>

              <tr>

                <td valign=center width="100%" bgcolor=#808080 colspan=2

                height=1>

                  <p align=center></p>

                </td>

              </tr>

              <tr>

                <td valign=center width="100%" bgcolor=#eeeeee colspan=2

                height=10></td>

              </tr>

              <tr>

                <td valign=center width="100%" bgcolor=#eeeeee

                  colspan=2 height="57">

                  <form method='post' action='member_search.html'>

                    <table width='95%' border='0' align='center' cellpadding='0' cellspacing='0' bordercolorlight='#000000' bordercolordark='#000000'>

                      <tr>

                        <td width='20%' height='34'>

                          <div align='center'>이름</div>

                        </td>

                        <td width='80%' height='34'>

                          <input type='text' name='search_name' size='20'>

                        </td>

                      </tr>

                      <tr>

                        <td height='34'>

                          <div align='center'>ID</font></div>

                        </td>

                        <td height='34'>

                          <input type='text' name='search_id' size='20'>

                          <input type='submit' name='search' value='[ 찾아보기 ]'>

                        </td>

                          <input type='hidden' name='task' value='process'>

 

                      </tr>

                    </table>

                  </form>

                  

                </td>

              </tr>

              <tr>

                <td valign=center width="100%" bgcolor=#eeeeee colspan=2

                height=10>

                </td>

              </tr>

                        <tr>

                <td valign=center width="100%" bgcolor=#808080 colspan=2

                height=1>

                  <p align=center></p>

                </td>

              </tr>

            </table>

<div align='center'><br>

  <font face='돋움' size='2' color='green'><a href='list.html'><font color='green'>★ 쪽지함 보기 ★</font></a></font> </div>

 

<?

}

else {

 

if (!$search_name && !$search_id) {

        echo (" <script>

        window.alert('아이디나 이름중 하나를 넣으세요')

        history.go(-1)

        </script>");

        exit;

}

 

 

$j = 0;

$query = "select id,name,email from member ";

if($search_id) {

        $query .= "where id like '%$search_id%' ";

        $j ++;

}

if($search_name) {

        if(!$j) $query .= "where ";

        else $query .= "AND ";

 

        $query .= "name like '%$search_name%' ";

}

 

$query .= "order by name ";

 

$Result = mysql_query($query);

if (!$Result) {      

        echo "

                <SCRIPT>

                alert('QUERY ERROR');

                history.go(-1);

                </SCRIPT>";

   exit;

}

 

$total = mysql_num_rows($Result);

 

//echo($query);

 

if (!$total){

        echo (" <script>

                window.alert('그런 아이디나 이름의 회원은 없는뎁슈??')

                history.go(-1)

                </script>");

} else {

 

echo "

<table width='387' border='0' cellspacing='0' cellpadding='0' vspace='0' hspace='0' align='center'>

<tr>

    <td><font size='2'><b><font color=#3a1563>내 친구 찾기 </font></td>

        <td align=right><font size='2'>검색결과 : $total 명</td>

    </tr>

</table>";

 

 

$counter = 0;

while ($counter < $total):

$id = mysql_result($Result, $counter, 0);

$name = mysql_result($Result, $counter, 1);

$email = mysql_result($Result, $counter, 2);

 

echo ("

<table width='387' border='0' cellspacing='0' cellpadding='0' vspace='0' hspace='0' align='center'>

  <tr bgcolor='#999999'>

    <td colspan='3'></td>

  </tr>

  <tr>

    <td bgcolor='#999999' width='1'></td>

    <td width='385' bgcolor='#F3F7FD'>  

      <table border='0' cellspacing='0' cellpadding='2' align='center' width='384'>

        <tr>

          <td height='55' rowspan='2' width='100'>

            <div align='center'><font size='2'>

              [ <b>$name</b> 님 ]</font></div>

          </td>

          <td align='right' rowspan='2'>

            <div align='left'>

              <font size='2'>★ i d : ");

        if($id) echo ("<a href='message.html?to_id=$id'>${id}</a>");

                          echo ("<br>

                ★ email :<b> $email<br>

               </font>

            </div>

            </td>

        </tr>

      </table>

    </td>

    <td bgcolor='#999999' width='1'></td>

  </tr>

  <tr bgcolor='#999999'>

    <td colspan='3'><img height='1' width='1' src='images/0.gif'></td>

  </tr>

</table>

<div align='right'>

  <font face='돋움' size='2' color='blac'><a href='list.html'><font color='green'>쪽지함 보기</font></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font> </div>

  <br>

");

 

$counter = $counter +1;

endwhile;

echo (" </table> ");

}

 

}

   

?>

</html>           

 

파일명 : message.html (쪽지 보내기)

 

<html>

<head>

<title>쪽지보내기</title>

<META http-equiv=content-type content=text/html; charset=euc-kr>

</head>

 

<body bgcolor="#FFFFFF" leftmargin="15" rightmargin="0" marginwidth="0" oncontextmenu="return false" marginheight="0">

 

<?

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

$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;

}

if($to_id) {

        $result = mysql_query("select name from member where id='$to_id' ");

        if(!$result) {

        echo "

                <SCRIPT>

                alert('QUERY ERROR');

                history.go(-1);

                </SCRIPT>";

                exit;

        }

 

        $total = mysql_num_rows($result);

                                  

        if ($total)

        {

                $to_name = mysql_result($result, 0, 0);

        }

}

if(!strcmp($task,"process")){

        

        if(!$total) {

                echo("

        <SCRIPT LANGUAGE='JavaScript'>

        <!--

        alert('$to_id 는 존재하지 않는 ID입니다.');

        location.replace('list.html');

        //-->

        </SCRIPT>

                ");

                exit;

 

        }

 

        $signdate = time();

        $message =  addslashes($message);

 

        $query = "INSERT INTO messages (from_id,to_id,message,signdate) ";

        $query .= "VALUES ('$user_id','$to_id','$message',$signdate)";

 

//echo("$query");

 

        $result = mysql_query($query);

        if (!$result) {      

        echo "

                <SCRIPT>

                alert('QUERY ERROR');

                history.go(-1);

                </SCRIPT>";

           exit;

        }

        echo ("<SCRIPT LANGUAGE='JavaScript'>

        <!--    

        location.replace('list.html');

        //-->

        </SCRIPT>");

        exit;

        

 

}else {

?>

<SCRIPT LANGUAGE='javascript'>

function chk_form()

{

        if(document.messageform.to_id.value == '') {

                        alert('ID을 입력하여 주세요.');

                        return;

                }

        else if(document.messageform.message.value.length < 0) {

                        alert('내용을 입력해주세요');

                        return;

                }

        else document.messageform.submit();

}

</SCRIPT>

<br>

  <form name='messageform' method='post' action='message.html'>

  <input type='hidden' name='task' value='process'>

  <table cellspacing=0 cellpadding=0 width='400' border=0>

    <tr>

      <td><font size='2'><b><font color=#3a1563> 쪽지보내자 </font></b></font></td>

      <td>

        <p align=right>&nbsp;</p>

      </td>

    </tr>

    <tr>

      <td valign=center width='100%' bgcolor=#eeeeee colspan=2

                height=10><img height=1 width=1></td>

    </tr>

    <tr>

      <td valign=center width='100%' bgcolor=#eeeeee

                  colspan=2 height='57'>

        <table width='88%' border='0' cellpadding='0' cellspacing='0' align='center' bordercolor='#2B97D5'>

          <tr>

            <td width='25%'>

              <div align='center'><font size='2'>To:(ID)</font></div>

            </td>

            <td width='81%'>

              <input type='text' name='to_id' size='20'  value='<?echo $to_id;?>'>

            </td>

          </tr>

          <tr>

            <td width='19%' height='65'>

              <div align='center'><font size='2'>보낼 내용</font></div>

            </td>

            <td width='81%' height='65'>

              <textarea name='message' rows='3' cols='30'  ></textarea>

            </td>

          </tr>

          <tr>

            <td colspan='2' height='29'>

              <div align='center'>

                <input type='button' name='Submit2' value='전송'  onclick='chk_form()' style='height:19px;background-color:#e3e3e3;border:1 solid black'>

                <input type='button' name='Reset' value='취소' onclick="javascript:location.replace('list.html')"  style='height:19px;background-color:#e3e3e3;border:1 solid black'>

              </div>

            </td>

          </tr>

        </table>

      </td>

    </tr>

  </table>

  <p>&nbsp;</p>

  <p>&nbsp;</p>

</form>

<?

}

?>

</html>

 

파일명 : delete.html (쪽지함 비우기)

<?

if(!$user_id) {

        echo "

                <SCRIPT>

                alert('로그인한 후에 이용하세요.');

                history.go(-1);

                </SCRIPT>";

        exit;

}

 

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

$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;

}

 

   $result = mysql_query("delete from messages where  to_id='$user_id' ");

   echo ("      <script>

   window.alert('쪽지함이 비워졌습니다.')

location.replace('list.html');

</script>");

  ?>

 

3. [문제] 보낸 쪽지 목록 보기

보낸 쪽지 목록을 볼 수 있는 프로그램을 작성하시오.

 

[목차]

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

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

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