구현내용
1. 댓글 리스트 출력
2. 글 작성자만 수정/삭제 버튼 보이기
3. 댓글 없을시 '등록된 댓글이 없습니다' 출력
4. 댓글 작성자 아이디(로그인한 아이디) 출력
5. 댓글 등록
comment db
| create table comment( cno int (10) not null auto_increment primary key, userid varchar(20)not null, content text not null, date date not null, bno int(10) ); |
read.php
<!-- 댓글 목록-->
<br/><br/>
<?php
$query_comment = "select * from comment where bno = '$bno'";
$result_comment = mysqli_query($connect, $query_comment);
$ctotal = $result_comment->num_rows;
if($ctotal == 0){
?> <tr>등록된 댓글이 없습니다</tr>
<?php
} else {
?>
<table class = "comment">
<colgroup border="1">
<col width="20%"/>
<col width="60%"/>
<col width="10%"/>
<col width="5%"/>
<col width="5%"/>
</colgroup>
<tr>
<td> <?php echo $row2['userid'] ?></td>
<td> <?php echo $row2['content'] ?></td>
<td> <?php echo $row2['date'] ?></td>
<?php
// 글 작성자만 버튼 활성화
if($_SESSION['userid'] == $row2['userid']){ ?>
<td> <button onclick="commentModify()">수정</button> </td>
<td>
<form method="post" action="c_delete_ok.php">
<input type="hidden" value="<?php echo $row['bno'] ?>" name=bno>
<input type="hidden" value="<?php echo $row2['cno'] ?>" name=cno>
<button type="submit" value="삭제">삭제</button>
</form>
</td>
<?php } ?>
</tr>
</table>
<?php }}
?>
<!-- 댓글 작성 -->
<form method="post" action="c_write_ok.php">
<table class="table">
<tr>
<td>작성자</td>
<td><input type="hidden" name=userid size=20 required> <?php echo $_SESSION['userid']?> </td>
<!--bno 넘길때 value값으로 안주고 그냥 input안에 입력해서 안넘어갔음 ㅠ -->
<td>
<input type="hidden" value="<?php echo $row['bno'] ?>" name=bno></td>
</tr>
<tr>
<td>내용</td>
<td><textarea name=content required></textarea ></td>
<td><button type = "submit" value="등록">등록</button>
</tr>
</table>
</form>
1. 쿼리문 : 현재글 번호에 맞는 comment select
select * from comment where bno = '$bno'
2. $ctotal에 num_rows를 통해 댓글 수 저장
3. $ctotal이 0 이라면 '댓글이 없습니다' 출력
4. 수정, 삭제 버튼은 글 작성자만 볼 수 있게
if($_SESSION['userid'] == $row2['userid']){ }
5. 댓글 작성하고 ok.php로 넘어가는데 값이 자꾸 안넘어와서 왠가 했더니 value값으로 지정했어야하는데
그냥 input문에 넣어놔서 값이 넘어오지 않았다 !
c_write_ok.php
<?php
include 'index.php';
session_start();
$URLlogin ='login.php';
if(!isset($_SESSION['userid'])){
?>
<!--세션 확인 후 없으면 로그인창 띄우기-->
<script>
alert("로그인이 필요합니다");
location.replace("<?php echo $URLlogin?>");
</script>
<?php }else{
$userid = $_SESSION['userid'];
$bno = $_POST[bno];
$content = $_POST['content'];
$date = date('Y-m-d H:i:s');
$URL = 'read.php?bno=';
print($URL);
$query = "insert into comment(cno,userid,content,date,bno)
values(null,'$userid','$content','$date','$bno')";
$result = mysqli_query($connect, $query);
?>
<?php } ?>
<script>
alert("<?php echo "댓글이 등록되었습니다."?>");
location.replace("<?php echo $URL.$bno?>");
</script>
insert문으로 댓글 삽입 !
