반응형

1. DB 스키마 구성(site3 데이터베이스의 article 테이블을 생성해주세요.)

테이블명 : article

article 테이블 칼럼 구성

id INT(10) UNSIGNED NOT NULL : 번호

AUTO_INCREMENT, PRIMARY KEY 옵션 추가

regDate DATETIME NOT NULL : 등록날짜

title CHAR(100) NOT NULL : 제목

body TEXT NOT NULL : 내용

hit INT(10) UNSIGNED NOT NULL : 조회수

DROP DATABASE IF EXISTS site1;
CREATE DATABASE site1;
USE site1;
CREATE TABLE article (
    id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    regDate DATETIME NOT NULL,
    title CHAR(100) NOT NULL,
    `body` TEXT NOT NULL,
    hit INT(10) UNSIGNED
);

 

2. config.php 파일 생성 (MySQL과 연결)

- 실행 http://localhost:8023/config.php

<?php
$Conn = mysqli_connect("ip주소", "loginId", "loginPw", "site3", 3036);


// 사용방법 (사용할 파일로가서 아래 코드 입력 후 진행)
<?php
require_once __DIR__ . '/../../config.php'; 

3. 게시글 작성 기능 (doWrite.php)

실행 : http://localhost:8023/article/doWrite.php?title=제목&body=내용

title이 제목이고, body가 내용인 새 글이 생성

<?php
require_once __DIR__ . '/../../config.php';

$title = $_GET['title'];
$body = $_GET['body'];

$sql = "
INSERT INTO article
SET regDate = NOW(),
title = '{$title}',
body = '{$body}'
";
mysqli_query($dbConn, $sql);

 

4. 게시글 리스팅 기능 (list.php)

실행 : http://localhost:8023/article/list.php

모든 게시물이 리스팅(HTML 테이블 태그 사용)

실행 : http://localhost:8023/article/list.php?searchKeyword=ㅋㅋ

title에 제목이라는 문구가 포함된 녀석이 모두 검색됨

SQL : SELECT * FROM article WHERE title LIKE '%ㅋㅋ%' ORDER BY id DESC;

<?php
require_once __DIR__ . '/../config.php';

if ( isset($_GET['searchKeyword']) == false ) {
    $sql = "
    SELECT *
    FROM article
    ORDER BY id DESC
    ";
} else {

    $searchKeyword = $_GET['searchKeyword'];
    $sql = "
    SELECT * 
    FROM article 
    WHERE title LIKE '%$searchKeyword%'
    ORDER BY id DESC;
    ";
}


$rs = mysqli_query($dbConn, $sql);

$articles = [];

while ( $row = mysqli_fetch_assoc($rs) ) {
    $rows[] = $row;
}

?>

<h1>게시물 리스트</h1>
<table border="1">
    <thead>
        <tr>
            <th>번호</th>
            <th>날짜</th>
            <th>조회수</th>
            <th>제목</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($rows as $row) { ?>
        <tr>
            <td>
                <a href="detail.php?id=<?=$row['id']?>">
                    <?=$row['id']?>
                </a>
            </td>
            <td>
                <?=$row['regDate']?>
            </td>
            <td>
                <?=$row['hit']?>
            </td>
            <td>
                <?=$row['title']?>
            </td>
        </tr>
        <?php } ?>
    </tbody>
</border>

 

5. 게시글 삭제 기능 (doDelete.php)

실행 : http://localhost:8023/article/doDelete.php?id=1

1번 게시물 삭제

SQL : delete from article where id = 1;

<?php
require_once __DIR__ . '/../../config.php';

$id = $_GET['id'];

$sql = "
DELETE FROM article
WHERE id = '{$id}'
";

mysqli_query($dbConn, $sql);

 

6. 게시글 수정 기능 (doModify.php)

실행 : http://localhost:8023/article/doModify.php?id=2&title=새 제목&body=새 내용

1번 게시물 수정(새 제목이고, body가 새 내용 으로 변경됨)

SQL : update article set title = '새 제목', body = '새 내용' where id = 2;

<?php
require_once __DIR__ . '/../../config.php';

$id = $_GET['id'];
$title = $_GET['title'];
$body = $_GET['body'];

$sql = "
UPDATE article
SET title = '{$title}',
body = '{$body}'
WHERE id = '{$id}'
";
mysqli_query($dbConn, $sql);

 

7. 게시글 디테일 기능 (detail.php)

실행 : http://localhost:8023/article/detail.php?id=3

3번글의 상세내용이 보여짐(HTML 테이블 태그 사용)

SQL : SELECT * FROM article WHERE id = 3;

SQL : UPDATE article SET hit = hit + 1 WHERE id = 3;

<?php
require_once __DIR__ . '/../../config.php';

$id = $_GET['id'];

$sql = "
UPDATE article
SET hit = hit + 1
WHERE id = '{$id}'
";

mysqli_query($dbConn, $sql);

$sql = "
SELECT *
FROM article
WHERE id = '{$id}'
";

$rs = mysqli_query($dbConn, $sql);
$article = mysqli_fetch_assoc($rs)

?>

<h1>게시물 리스트</h1>

<?php if ( $article == null) { ?>
<h2><?=$id?>번 게시물이 존재하지 않습니다.</h2>
<?php exit; } ?>

<table border="1">
    <tbody>
        <tr>
            <th>번호</th>
            <td><?=$article['id']?></td>
        </tr>
        <tr>
            <th>날짜</th>
            <td><?=$article['regDate']?></td>
        </tr>
        <tr>
            <th>조회수</th>
            <td><?=$article['hit']?></td>
        </tr>
        <tr>
            <th>제목</th>
            <td><?=$article['title']?></td>
        </tr>
        <tr>
            <th>본문</th>
            <td><?=$article['title']?></td>
        </tr>
        <tr>
            <th>비고</th>
            <td>
                <a href="doDelete.php?id=<?=$article['id']?>">삭제</a>
                <a href="list.php">리스트</a>
            </td>
        </tr>
    </tbody>
</border>
반응형

+ Recent posts