본문 바로가기
IT/Database

DB 게시판 만들기

by hjshims 2021. 6. 8.

write.html  //글쓰기

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">

  <title>게시판</title>
 </head>
 <body>
	<h3> 글 쓰 기 </h3>
	<hr>
	<form action="writePorc.php" method="POST">
	<table width=600 cellpadding=2 cellsacing=1 bgcolor="#336699">
		<tr>
			<td colspan=2 ><font color="white">글 쓰 기</font></td>
		</tr>
		<tr bgcolor="#f2ffff">
			<td>제목</td>
			<td><input type="text" name="title" size=70 maxlength=4 /></td>
		</tr>
		<tr bgcolor="#f2ffff">
			<td>내용</td>
			<td><textarea name="content" cols="70" rows="15"></textarea> </td>
		</tr>
		<tr bgcolor="#f2ffff">
			<td>작성자</td>
			<td><input type="text" name="name" size=20 maxlength=20 /></td>
		</tr>
		<tr bgcolor="#f2ffff">
			<td colspan=2 align=right height=30>
				<input type="submit" value="저장"/> &nbsp;&nbsp;
				<input type="reset" value="취소"/> &nbsp;&nbsp;
				<input type="button" value="이전" onClick="history.back(1)"/>
			</td>
		</tr> 
	</table>
		
	</form>
 </body>
</html>

 

writeProc.php  //글쓰기

<?php 
$conn=mysqli_connect("127.0.0.1", "root", "", "mydb");
mysqli_query($conn, "set names utf8-general-ci");
mysqli_query($conn, "insert into board(name, title, content, wdate, hit) 
				values('$_POST[name]','$_POST[title]','$_POST[content]', now(), 0)");
mysqli_close($conn);
?>

 

list.php  //리스트출력

<?php
$conn=mysqli_connect("127.0.0.1", "root", "", "mydb");
mysqli_query($conn, "set names utf8-general-ci");
$result=mysqli_query($conn,"select * from board order by num DESC");
?>

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>게시판</title>
 </head>
 <body>
  <h1>자유게시판</h1>
  <table>
	<tr>
		<th width=70>번호</th>
		<th width=500>제목</th>
		<th width=120>작성자</th>
		<th width=100>작성일</th>
		<th width=100>조회수</th>
	</tr>
<?php
	while( $row=mysqli_fetch_array($result)) {
?>
	<tr>
		<td width=70><?php echo $row['num'] ?> </td>
		<td width=70><a href="read.php?no=<?=$row['num'] ?>" > <?php echo $row['title'] ?> </a> </td>
		<td width=70><?php echo $row['name'] ?> </td>
		<td width=70><?php echo $row['wdate'] ?> </td>
		<td width=70><?php echo $row['hit'] ?> </td>
	</tr>
<?php
}
?>
  </table>
 </body>
</html>

 

read.php //읽어오기

<?php
$conn=mysqli_connect("127.0.0.1", "root", "", "mydb");
mysqli_query($conn, "set names utf8-general-ci");
$result=mysqli_query($conn,"select * from board where num='$_GET[no]' ");
$row=mysqli_fetch_array($result);
echo "번호 : ".$row['num']."<br>";
echo "제목 : ".$row['title']."<br>";
echo "내용 : ".$row['content']."<br>";
echo "작성자 : ".$row['name']."<br>";
echo "작성일 : ".$row['wdate']."<br>";
echo "조회수 : ".$row['hit']."<br>";
?>
<hr>
<a href="list.php">목록</a> &nbsp;&nbsp;&nbsp;
<a href="write.php">글쓰기</a> &nbsp;&nbsp;&nbsp;
<a href="edit.php?no=<?=$row['num'] ?>">수정</a> &nbsp;&nbsp;&nbsp;
<a href="delete.php?no=<?=$row['num'] ?>">삭제</a> &nbsp;&nbsp;&nbsp;
<hr>
<?php
$cnt = $row['hit'] + 1;
mysqli_query($conn, "update board set hit=$cnt where num='$row[num]'");
?>

 

edit.php //수정

<?php
$conn=mysqli_connect("127.0.0.1", "root", "", "mydb");
mysqli_query($conn, "set names utf8-general-ci");
$result=mysqli_query($conn,"select * from board where num='$_GET[no]' ");	//GET방식으로 가져온 no를 17번줄의
$row=mysqli_fetch_array($result);
?>

<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>게시판</title>
 </head>
 <body>
	<h3> 글 수 정 </h3>
	<hr>
	<form action="editPorc.php?no=<?=$_GET['no'] ?>" method="POST">			<!--여기서 그대로 no를 가져감-->
		이름 : <input type="text" name="name", value="<?=$row['name'] ?>" size=70> <br>
		제목 : <input type="text" name="title", value="<?=$row['title'] ?>" size=70> <br>
		내용 : <textarea name="content", cols="70" rows="15"> <?=$row['content'] ?> </textarea> <br>
		<hr>
		<input type="submit" value="저장"> &nbsp;&nbsp;&nbsp;
		<input type="reset" value="취소"> &nbsp;&nbsp;&nbsp;
		<input type="button" value="이전" onClick="history.back(1)">
	</form>
 </body>
</html>

 

editProc.php //수정

<?php 
$conn=mysqli_connect("127.0.0.1", "root", "", "mydb");
mysqli_query($conn, "set names utf8-general-ci");
mysqli_query($conn, "update board set name='$_POST[name]', title='$_POST[title]', content='$_POST[content]'
			where num=$_GET[no]");

mysqli_close($conn);
?>
<meta http-equiv='refresh' content="1; url=read.php?no=<?=$_GET['no']?>" >

 

delete.php //삭제

<?php 
$conn=mysqli_connect("127.0.0.1", "root", "", "mydb");
mysqli_query($conn, "set names utf8-general-ci");
mysqli_query($conn, "delete from board where num='$_GET[no]'");

mysqli_close($conn);
?>
<meta http-equiv='refresh' content="1; url=list.php" >

 

write.html
list.php