수업관련

php codes

김대니 2020. 5. 27. 01:33
//sql

DROP DATABASE IF EXISTS site3;

CREATE DATABASE site3;

USE site3;

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) NOT NULL
)
<?php 

//list


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

if(isset($_GET['searchKeyword'])==false){
    $_GET['searchKeyword']= '';
}

$searchKeyword = $_GET['searchKeyword'];


$rq = "
SELECT *
FROM article;
";
if (!empty($searchKeyword)){
    $sql .="
    AND title LIKE '%{$searchKeyword}%'
    ";
}
$rs = mysqli_query($dbConn,$rq);

$sql ="
SELECT *
FROM article
ORDER BY id DESC
LIMIT 100;
";
$rs = mysqli_query($dbConn,$sql);
$articles = [];

while ( $article = mysqli_fetch_assoc($rs)) {
    $articles[] = $article;
}
?>
<h1>게시물 리스트</h1>

<table border="1">
    <thead>
        <tr>
            <th>번호</th>
            <th>조회수</th>
            <th>날짜</th>
            <th>제목</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach (  $articles as  $article ) { ?>
        <tr>
            <td><?=$article['id']?></td>
            <td><?=$article['hit']?></td>
            <td><?=$article['regDate']?></td>
            <td>
                <a href="detail.php?id=<?=$article['id']?>">
                    <?=$article['title']?>
                </a>
            </td>
        </tr>
        <?php } ?>
    </tbody>
</table>​
<?php

//doModify

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

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

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

$rs = mysqli_query($dbConn,$sql);​
<?php

//doWrite

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);
<?php

//doDelete

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

$id = $_GET['id'];

$sql = "
DELETE FROM article
WHERE id = '{$id}';
";
mysqli_query($dbConn, $sql);
<?php

//detail


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

$id = $_GET['id'];

$rq = "
UPDATE areticle
SET hit = hit + 1 
WHERE id = '{$id}';
";
mysqli_query($dbConn,$rq);

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

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

$article = mysqli_fetch_assoc($rs)
?>

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

<table border="1">
    <tbody>
        <tr>
            <th>번호</th>
            <td> <?=$article['id'] ?> </td>
        </tr><tr>
            <th>조회수</th>
            <td><?=$article['hit']?></td>
            </tr><tr>
            <th>날짜</th>
            <td><?=$article['regDate']?></td>
            </tr><tr>
            <th>제목</th>
            <td><?=$article['title']?></td>
            </tr><tr>
            <th>내용</th>
            <td><?=$article['body']?></td>
        </tr>
   </tbody>
</table>