반응형
테이블 구조
conn.php
<?php
$conn = new PDO('mysql:host=localhost;dbname={db이름}', '{mysql계정}', '{mysql비번}', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
?>
#예
<?php
$conn = new PDO('mysql:host=localhost;dbname=board', 'root', '1234', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
?>
Create - 글 쓰기
write.html
<form action="./insert.php" method="POST">
제목 : <input type="text" name="title">
내용: <input type="text" name="sub">
<button>제출</button>
</form>
insert.php
<?php
include 'conn.php';
$title = $_POST["title"];
$sub = $_POST["sub"];
$sql_write = $conn->prepare("INSERT INTO text (title,sub,date) VALUES (:title, :sub, now())");
$sql_write->bindParam(':title',$title);
$sql_write->bindParam(':sub',$sub);
$sql_write->execute();
echo '<script>alert("작성되었습니다.");history.back();</script>';
?>
Read - 글 보기
board.php
<h1>전체 글</h1>
<table border=1>
<tr>
<td>no.</td>
<td>제목</td>
<td>작성 날짜</td>
</tr>
<?php
include 'conn.php';
$sql_list_all = $conn->prepare("SELECT * FROM text");
$sql_list_all->execute();
$list = $sql_list_all->fetchAll(PDO::FETCH_ASSOC);
foreach($list as $row){
?>
<tr>
<td>
<?php echo htmlspecialchars($row['id']); ?>
</td>
<td>
<a href="./info.php?id=<?php echo($row['id']); ?>">
<?php echo htmlspecialchars($row['title']); ?></a>
</td>
<td>
<?php echo htmlspecialchars($row['date']); ?>
</td>
</tr>
<?php } ?>
</table>
Update - 글 수정
info.php
<?php
include 'conn.php';
$id = $_GET['id'];
$sql_board = $conn->prepare("SELECT * FROM text WHERE id = :id");
$sql_board->bindValue(':id', $id);
$sql_board->execute();
$board = $sql_board->fetchAll(PDO::FETCH_ASSOC);
foreach($board as $row){
?>
<h1><?php echo $row["id"]; ?>번 글</h1>
<table>
<form action="./update.php?id=<?php echo $id; ?>" method="POST">
<tr>
<td>제목</td>
<td><input type="text" name="title" value="<?php echo $row["title"]; ?>"></td>
</tr>
<tr>
<td>내용</td>
<td><textarea name="sub" cols="30" rows="10"><?php echo $row["sub"]; ?></textarea></td>
</tr>
<tr>
<td><input type="submit" value="수정"></td>
<td></td>
</tr>
</form>
</table>
<button onclick="location.href='./delete.php?id=<?php echo $id; ?>';">삭제</button>
<?php } ?>
update.php
<?php
include 'conn.php';
$title = $_POST["title"];
$sub = $_POST["sub"];
$id = $_GET["id"];
$sql_update = $conn->prepare("UPDATE text SET title = :title, sub = :sub, date = now() WHERE id = :id");
$sql_update->bindValue(':title', $title);
$sql_update->bindValue(':sub', $sub);
$sql_update->bindValue(':id',$id);
$sql_update->execute();
echo '<script> alert("수정되었습니다."); history.back();</script>';
?>
Delete - 글 삭제
delete.php
<?php
include 'conn.php';
$id = $_GET["id"];
$sql_delete = $conn->prepare("DELETE FROM text WHERE id = :id");
$sql_delete->bindValue(':id', $id);
$sql_delete->execute();
echo '<script>alert("삭제되었습니다."); location.href="./result.php";</script>';
?>
result.php
<h1>성공!</h1>
반응형
'📁Language > 🟣 PHP' 카테고리의 다른 글
[PHP] Window에서 개발환경 만들기 -php,apache,mysql다 따로 다운받기 (7) | 2022.01.22 |
---|---|
[PHP] include (0) | 2021.10.22 |
[PHP] 배열 (0) | 2021.10.20 |
[PHP] 함수 (0) | 2021.10.20 |
[PHP] 조건문, 반복문 (0) | 2021.10.20 |