connect/connect.php 에 'MySQL' 작성
<?php
$host = "localhost";
$user = "사용자아이디";
$pw = "패스워드";
$db = "사용자아이디";
$connect = new mysqli($host, $user, $pw, $db);
$connect -> set_charset("utf8");
if(mysqli_connect_errno()){
echo "Database Connect False";
} else {
//echo "Database Connect True";
}
?>
1. comment/comment.php (전송된 데이터 출력 & HTML 양식저장)
<div class="comment-list">
<?php
include "../connect/connect.php";
$sql = "SELECT * FROM myComment";
$result = $connect -> query($sql);
//반복문을 통해 결과값 데이터 가져오기
while($date = mysqli_fetch_array($result)){
?>
<div>
<p><?=$date['youText']?></p>
<div class="icon">
<img src="../assets/img/stu01.png" alt="조서은">
<span><?=$date['youName']?></span>
<span><?=date('Y-m-d H:i', $date['regTime'])?></span>
</div>
</div>
<?php
}
?>
<!-- <div>
<p> 깔끔한 설명에 이해가 잘됐어요!
항상 잘 보고 있답니당👍</p>
<div class="icon">
<img src="../assets/img/stu01.png" alt="조서은">
<span>#ksys**</span>
</div>
</div> -->
</div>
공통 HTML 오려두기 (php로 따로 저장)
2. create/createComment.php
<?php
include "../connect/connect.php";
$sql = "CREATE TABLE studyComment (";
$sql .= "studyCommentID int(10) unsigned NOT NULL AUTO_INCREMENT,";
$sql .= "youName varchar(20) NOT NULL,";
$sql .= "youText varchar(50) NOT NULL,";
$sql .= "regTime int(15) NOT NULL,";
$sql .= "PRIMARY KEY (studyCommentID)) CHARSET=utf8";
$result = $connect -> query($sql);
if( $result ){
echo "Create Comment Complete";
} else {
echo "Create Comment False";
}
?>
3. comment/commentSave.php (댓글쓰기 데이터 입력)
<?php
include "../connect/connect.php";
$youName = $_POST['youName'];
$youText = $_POST['youText'];
$regTime = time();
// echo $youName;
// echo $youText;
$sql = "INSERT INTO myComment(youName, youText, regTime) VALUES('$youName', '$youText', '$regTime')";
$result = $connect -> query($sql);
if($result){
echo "INSERT INTO 'true'";
}else{
echo "INSERT INTO 'false'";
}
?>
//db에 전송된 내용을 출력
<script>
location.href = "comment.php#comment";
</script>