티스토리 뷰
다음은 글쓰기 버튼을 클릭했을 때 새로운 글을 작성할 수 있는 코드입니다.
게시판 전체 코드는 https://github.com/dhffll/php-Mysql-Board에서 확인하실 수 있습니다.
create.php
<?php
include 'header.php';
?>
<h1><a href="index.php">Board</a></h1>
<form action="create2.php" method="POST">
<p><input type="text" name="title" placeholder="title"></p>
<textarea name="content" placeholder="content"></textarea>
<p><input type="submit" value="submit"></p>
</form>
<a href="index.php">cancel</a>
<?php
include 'footer.php';
?>
작성한 게시물을 create2.php 파일로 폼 전송해줍니다.
create2.php
<?php
include 'mysql.php'; //DB 연결하는 코드 $con
session_start();
$filtered = [
"title" => mysqli_real_escape_string($con,$_POST['title']),
"content" => mysqli_real_escape_string($con,$_POST['content'])
];
$sql = "
insert into board(
title, writer, content, date
)
values(
'{$filtered['title']}',
'{$_SESSION['id']}',
'{$filtered['content']}',
now()
)
";
$result = mysqli_query($con,$sql);
if($result){
header("location: index.php");
}else{
echo mysqli_error($con);
}
?>
form으로 받아오면 게시물의 데이터를 테이블에 저장합니다.
글 작성이 완료되었다면 게시물을 보여줍시다.
index.php
<?php
include 'mysql.php'; //DB 연결 $con
include 'header.php';
$sql = "select * from board";
$result = mysqli_query($con,$sql);
?>
<?php
if (empty($_SESSION['id'])) {
?>
<a href='login.php'>login</a>
<a href='resister.php'>signup</a>
<?php
} else {
?>
<span>Welcome, <?= $_SESSION['id'] ?>!</span>
<a href='create.php'>write</a>
<a href='logout.php'>logout</a>
<a href="change_info.php">info</a>
<?php
}
?>
<h1><a href="index.php">Board</a></h1><br>
<table>
<tr>
<td>날짜</td>
<td style="width:200px">글제목</td>
<td>작성자</td>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
$filtered = [
'date' => date('m-d', strtotime(htmlspecialchars($row['date']))),
'title' => htmlspecialchars($row['title']),
'writer' => htmlspecialchars($row['writer']),
'num' => htmlspecialchars($row['num'])
];
}
?>
<tr>
<td><?= $filtered['date'] ?></td>
<td><a href="list.php?num=<?= $filtered['num'] ?>"><?= $filtered['title'] ?></a></td>
<td><?= $filtered['writer'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
include 'footer.php';
?>
테이블에 저장된 게시물을 모두 확인할 수 있습니다.
다음은 삭제, 수정하는 기능을 넣어보겠습니다.
'php' 카테고리의 다른 글
| [php] 게시판 구현하기 - 수정/삭제 (update/delete) (0) | 2022.04.29 |
|---|---|
| [php] 게시판 만들기 - 페이징, 검색, 정렬 구현하기 (0) | 2022.04.25 |
| [php] 게시판 구현하기 - 비밀번호 변경하기 (0) | 2022.04.21 |
| [php] 게시판 구현하기 - 회원 가입/아이디 중복 확인/비밀번호 확인 (0) | 2022.04.20 |
| [php] 게시판 구현하기 - 로그인/로그아웃 (0) | 2022.04.19 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- JS session
- php composer 설치
- fetch get post 전송
- JavaScript
- php rand()
- input placeholder
- 링크 공유 시 정보 수정
- php mysql 랜덤 숫자
- html input
- curl.cainfo 적용
- http 접속 시 https 리다이렉션
- 링크 공유 시 썸네일
- AJAX
- fetch post 데이터 전송
- 자바스크립트
- react fetch
- JS 시계 기능
- 카톡 링크 공유 썸네일
- php 게시판
- JS url 파라미터
- 자바스크립트 url 파라미터 추출
- 자바스크립트 세션
- php composer
- curl ssl 인증서 다운로드
- HTML
- react restful API
- curl.cainfo
- curl.cainfo 에러
- php mongodb
- curl ssl 인증서
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함