2023. 5. 7. 11:26ㆍWEB/PHP
수정과 삭제 모두 똑같습니다.
전혀~다를게 없고 전혀~어려울게 없습니다.
조회랑 똑같습니다. 쿼리만 약간 달라지는거에요~!
특정 게시글 조회가 select * from board where bno = ? 이라면
특정 게시글 삭제는 delete from board where bno = ? 입니다.
특정 게시글 수정은 어떨까요? 네 update board set ~~~ where bno = ? 입니다.
거의 대가리만 달라진다 이소립니다!
바로 만들어봅시다.
먼저 쿼리문을 생각해보죠.
바뀔것은 title,content,updatedate(수정시간)입니다.
update from board set title = ?,content = ?,updatedate = ? where bno = ?
이제 board 디렉토리에 각각 update.php를 만들어주시고
먼저 입력 양식입니다. 뭘까요? 바로 게시글 등록 페이지와 판박이입니다. 일절 다를게 없어요!
<!DOCTYPE html>
<html>
<head>
<title>Board Modify</title>
</head>
<body>
<h1>Modify page</h1>
<form method="post" action="">
<input type="text" name = "title" placeholder="Enter title">
<textarea name = "content" rows="10" cols="100" placeholder="Enter content"></textarea>
<br>
<input type="submit" value="write"></input>
</form>
</body>
</html>
자 이제 PHP 코드... 똑같이 DB연결과 쿼리문 제작...
<?php include "../connect.php"; ?>
<?php
$sql = "update board set title = '$title',content = '$content',updatedate = '$updatedate'
where bno = $bno";
?>
네 쿼리문은 제작했는데..title과 content는 어디서 가져올까요?
수정은 전에 있던 정보를 수정하는것이죠? 네 그러니까 select 먼저 해야한다는 사실입니다.
좀 수정을 하죠.
<?php include "../connect.php"; ?>
<?php
$bno = $_GET['bno'];
$sql = "select * from board where bno = '$bno'";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$title = $row['title'];
$content = $row['content'];
}
?>
됬습니다. 이제 프론트에 이 title과 content를 가져가야 합니다.
<!DOCTYPE html>
<html>
<head>
<title>Board Modify</title>
</head>
<body>
<h1>Modify page</h1>
<form method="post" action="">
<input type="text" name = "title" placeholder="Enter title" value = <?php echo $title; ?>>
<textarea name = "content" rows="10" cols="100" placeholder="Enter content"><?php echo $content; ?>
</textarea>
<br>
<input type="submit" value="write"></input>
</form>
</body>
</html>
이런식으로 글의 내용이 잘 출력이 되고
이런식으로 글이 잘 수정이 되는것을 볼 수 있습니다. 이제 write 버튼을 누르면 입력폼에 있는데이터로
실제 데이터를 수정해봅시다.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'];
$content = $_POST['content'];
$updatedate = date('Y-m-d');
$sql = "update board set title = '$title',content = '$content',updatedate = '$updatedate'
where bno = $bno";
$result = mysqli_query($conn,$sql);
if($result){
echo "<script>location.href = 'read.php?bno=$bno';</script>";
}
}
요청 방식이 POST 일때 입력된 데이터를 불러와서 업데이트 구문을 만들고 실행합니다.
수정이 완료되면, 수정된 게시물을 볼 수 있도록 bno를 가져와서 read 페이지로 이동합니다.
저는 5번 게시물을 수정 해 보겠습니다.
수정을 누르자 read 페이지로 바뀐것을 보여줍니다.
'WEB > PHP' 카테고리의 다른 글
PHP - 게시판 제작 6 - 게시글 삭제 (0) | 2023.05.07 |
---|---|
PHP - 게시판 제작 4 - 게시글 작성 (0) | 2023.05.06 |
PHP - 게시판 제작 3 - 조회 페이지 제작 (0) | 2023.05.05 |