글쓰기 / 관리자
헤니l
헤니의 개발 공부
헤니l
전체 방문자
오늘
어제
  • 분류 전체보기 (100)
    • study (46)
      • JAVA (5)
      • PHP (24)
      • JS , jQuery (7)
      • 서버 (1)
      • 코딩테스트 (9)
    • 회사일기 (1)
    • 취준 (2)
    • 인천일보아카데미 교육과정 (51)
      • 인천일보아카데미 (51)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 비전공개발공부
  • php계층형게시판
  • 자바스크립트
  • php게시판리스트
  • JS
  • HTML
  • php게시판
  • 비전공코딩공부
  • 비전공개발자
  • 게시판구현
  • 날씨어플만들기
  • 인천일보아카데미
  • HTML공부
  • 코딩
  • CSS
  • 코딩공부
  • 비전공자개발공부
  • php회원가입
  • PHP
  • 개발공부

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
헤니l

헤니의 개발 공부

[PHP] 게시판 댓글 읽기, 쓰기
study/PHP

[PHP] 게시판 댓글 읽기, 쓰기

2022. 12. 29. 13:39

구현내용

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문으로 댓글 삽입 ! 

 

 

 

 

댓글 출력, 댓글 작성창

    'study/PHP' 카테고리의 다른 글
    • [PHP] 계층형 게시판(답글) 로직
    • [PHP] 게시판 댓글 삭제
    • [PHP] 게시판 글 수정,삭제
    • [PHP] 게시판 리스트 페이징
    헤니l
    헤니l

    티스토리툴바