본문 바로가기
Project/APM웹서버

게시판 만들기 ( DB 데이터 불러오기 )

by Albin 2021. 11. 8.

Union SQL Injection, Error Based SQL Injection 을 공부하기 위해 게시판을 만들기로 했다.

 

Board.php

<!DOCTYPE html>
<html>
<head>
        <meta charset = 'utf-8'>
</head>
<style>
table{
        border-top: 1px solid #444444;
        border-collapse: collapse;
}
tr{
        border-bottom: 1px solid #444444;
        padding: 10px;
}
td{
        border-bottom: 1px solid #efefef;
        padding: 10px;
}
table .even{
        background: #efefef;
}
.text{
        text-align:center;
        padding-top:20px;
        color:#000000
}
.text:hover{
        text-decoration: underline;
}
a:link {color : #57A0EE; text-decoration:none;}
a:hover { text-decoration : underline;}
</style>
<body>
	<?php
		$mysqli = mysqli_connect("127.0.0.1","admin0","0000","normaltic1","3306");
        if ($mysqli->connect_errno) {
        	die('Connect Error: '.$mysqli->connect_error);
		}
		$query ="select * from boardList order by number desc";
		$result = $mysqli->query($query);
		$total = mysqli_num_rows($result);
	?>
    <h2 align=center>게시판</h2>
    <table align = center>
        <thead align = "center">
                <tr>
                        <td width = "50" align="center">번호</td>
                        <td width = "500" align = "center">제목</td>
                        <td width = "100" align = "center">작성자</td>
                        <td width = "200" align = "center">날짜</td>
                        <td width = "50" align = "center">조회수</td>
                </tr>
        </thead>

<tbody>
	<?php
	while($rows = mysqli_fetch_assoc($result)){ //DB에 저장된 데이터 수 (열 기준)
	if($total%2==0){
	?>	<tr class = "even">
	
    <?php   }
	else{
	?>	<tr>
	<?php } ?>
	<td width = "50" align = "center"><?php echo $total?></td>
	<td width = "500" align = "center">
	<a href = "view.php?number=<?php echo $rows['number']?>">
	<?php echo $rows['title']?></td>
	<td width = "100" align = "center"><?php echo $rows['id']?></td>
	<td width = "200" align = "center"><?php echo $rows['date']?></td>
	<td width = "50" align = "center"><?php echo $rows['hit']?></td>
	</tr>
	<?php
	$total--;
	}	?>
</tbody>
</table>
	<div class = text>
	<font style="cursor: hand"onClick="location.href='./write.php'">글쓰기</font>
	</div>
	</body>
</html>

 

 

이전에 데이터베이스에 임의로 넣어 두었던 값이 정상출력 되는걸 확인 할 수 있다.

 

이전에 태그 사이에 php 코드를 넣으려고 하다가 동작되는 시점이 달라 불가능 한 줄 알았는데, 오늘 참고한 글에서 가독성은 많이 떨어 지지만 가능한 것을 확인 하였다.

 

그리고 몇몇 글들에서는 db연결기능을 하는 php 따로하여 include 하여 이후에 db 설정이 달라지거나 했을때 일괄적으로 변경 되도록 하는 코드도 확인 했는데, php가 좀더 숙달 된다면 반복적으로 쓰이는 기능들을 클래스 단위로 빼서 작성하는 것도 해봐야 겠다.

 

다음글 에서는 직접 db에 작성한 글을 insert 하는 작업을 해보겠다.

 

참고

https://chamggae.tistory.com/80?category=793467 

 

[ php / mysql ] 게시판 만들기 (목차)

목차 부분 코드입니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62..

chamggae.tistory.com