1) 베스트 셀러 등록, 수정, 삭제

best.php
<?php include_once 'include/header.php' ?>
<?php
define($total, 0);
$conn = mysqli_connect('localhost','root','0115','test');
$query = "select * from bestsell";
$result = mysqli_query($conn, $query);
$total = mysqli_num_rows($result);
// echo $total;
// 한 페이지당 레코드 개수
$list_num = 8;
// 한 블럭당 페이지 수
$page_num = 2;
// 현재 페이지
$page = isset($_GET['page']) ? $_GET['page'] : 1;
// 전체 페이지수 = 전체 레코드 수 / 한 페이지당 레코드 개수
// 전체 데이터가 20개라 하면, 20 / 5 => 4
$total_page = ceil($total / $list_num);
// 전체 블럭수 = 전체 페이지 수 / 블럭당 페이지 수
$total_block = ceil($total_page / $page_num);
// 현재 블럭 번호 = 현재 페이지 번호/ 블럭당 페이지 수
// 1 / 3 1 7 / 3
$now_block = ceil($page / $page_num);
// 블럭 당 시작 페이지 번호 = (해당글의 블럭번호 -1) * 블럭당 페이지수 + 1
$s_pageNum = ($now_block -1 ) * $page_num+1;
// 데이터가 0개인 경우
if($s_pageNum <= 0) {
$s_pageNum = 1;
}
// 블럭 당 마지막 페이지 번호
$e_pageNum = $now_block * $page_num;
// 마지막 페이지 번호가 전체 페이지 수를 넘지 않도록 설정
if($e_pageNum > $total_page) {
$e_pageNum = $total_page;
}
// 시작번호 0, 5, 10, 15 ...
$start = ($page-1) * $list_num;
// 쿼리작성
$sql = "select * from bestsell limit $start, $list_num;";
$result2 = mysqli_query($conn, $sql);
function printList() {
global $result2;
global $total;
while($row = mysqli_fetch_array($result2)){
echo "<tr>
<td>{$row['id']}</td>
<td><img src=.//{$row['imgroot']}></td>
<td><a href=\"best_detail.php?id={$row['id']}\">{$row['imgname']}</a></td>
<td>{$row['writer']}</td>
<td>{$row['publisher']}</td>
<td>{$row['price']}원</td>
<td>{$row['bookdate']}</td>
</tr>";
}
}
?>
<div id="contents_page" class="inner">
<h2>베스트 셀러 목록</h2>
<h3>베스트 셀러 목록입니다. </h3>
<table>
<tr>
<th>아이디</th>
<th>이미지</th>
<th>제목</th>
<th>글쓴이</th>
<th>출판사</th>
<th>가격</th>
<th>출판일</th>
</tr>
<?php printList(); ?>
</table>
<p class="pager">
<!-- 이전버튼 -->
<?php
if($page <= 1){ ?>
<a href="best.php?page=1">이전</a>
<?php
}
else{ ?>
<a href="best.php?page=<?=($page-1)?>">이전</a>
<?php
}
?>
<?php
for($print_page = $s_pageNum; $print_page <= $e_pageNum; $print_page++) {
?>
<a href="best.php?page=<?=$print_page?>"><?=$print_page?></a>
<?php
}
?>
<!-- 다음버튼 -->
<?php
if($page >= $total_page){
?>
<a href="best.php?page=<?=$total_page?>">다음</a>
<?php
}else{
?>
<a href="best.php?page=<?=($page+1)?>">다음</a>
<?php
}
?>
</p>
<div id="searchDiv">
<form action="search.php" method="post">
<span>검색하기</span>
<select name="search_m" id="search">
<option value="title">제목</option>
<option value="writer">글쓴이</option>
<option value="publisher">출판사</option>
</select>
<input type="text" name="search" required>
<button id="searchBtn">검색하기</button>
</form>
<button id="rightBtn"><a href="best_create.php">도서등록</a></button>
</div>
</div>
<?php include_once 'include/footer.php' ?>
best_create.php
<?php include_once 'include/header.php' ?>
<div id="write_book" class="inner">
<h2>베스트 셀러 등록</h2>
<h3>베스트 셀러를 등록하세요.</h3>
<form action="process/process_best_create.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>이미지</td>
<td><input type="file" name="imgroot" required></td>
</tr>
<tr>
<td>글쓴이</td>
<td><input type="text" name="writer" required></td>
</tr>
<tr>
<td>출판사</td>
<td><input type="text" name="publisher" required></td>
</tr>
<tr>
<td>가격</td>
<td><input type="text" name="price" required id="priceInput"></td>
</tr>
<tr>
<td>출판일</td>
<td><input type="text" name="bookdate"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="imgname"></td>
</tr>
<tr>
<td colspan="2">
<button>도서등록</button>
<button>취소</button>
</td>
</tr>
</table>
</form>
<script>
let priceInput = document.querySelector('#priceInput');
priceInput.addEventListener('input', function(){
if(isNaN(Number(priceInput.value))){
alert("가격은 숫자만 입력해주세요.");
priceInput.value="";
}
})
</script>
<?php include_once 'include/footer.php' ?>
best_detail.php
<?php include_once 'include/header.php' ?>
<?php
$bookid = $_GET['id'];
$conn = mysqli_connect('localhost', 'root', '0115', 'test');
$query = "select * from bestsell where id={$bookid}";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
?>
<div id="write_book" class="inner">
<h2>베스트 셀러 자세히보기</h2>
<h3>베스트 셀러 내용입니다.</h3>
<table>
<tr>
<td>글쓴이</td>
<td><img src=.//<?=$row['imgroot']?>></td>
</tr>
<tr>
<td>글쓴이</td>
<td><?=$row['writer']?></td>
</tr>
<tr>
<td>출판사</td>
<td><?=$row['publisher']?></td>
</tr>
<tr>
<td>출판일자</td>
<td><?=$row['bookdate']?></td>
</tr>
<tr>
<td>제목</td>
<td><?=$row['imgname']?></td>
</tr>
<tr>
<td>내용</td>
<td><?=$row['desc']?></td>
</tr>
<tr>
<td colspan="2">
<form action="best_edit.php" method="post">
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<button type="submit">수정</button>
</form>
<form action="process/process_best_delete.php" method="post">
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<button type="submit">삭제</button>
</form>
</td>
</tr>
</table>
</div>
<?php include_once 'include/footer.php' ?>
best_edit.php
<?php include_once 'include/header.php' ?>
<?php
$bookid = $_POST['id'];
$conn = mysqli_connect('localhost', 'root', '0115', 'test');
$query = "select * from bestsell where id={$bookid}";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
?>
<div id="write_book" class="inner">
<h2>도서수정</h2>
<h3>도서를 수정하세요.</h3>
<form action="process/process_best_edit.php?id=<?=$_POST['id']?>" method="post">
<table>
<tr>
<td>글쓴이</td>
<td><input type="text" name="writer" value="<?=$row['writer']?>"></td>
</tr>
<tr>
<td>출판사</td>
<td><input type="text" name="publisher" value="<?=$row['publisher']?>"></td>
</tr>
<tr>
<td>가격</td>
<td><input type="text" name="price" value="<?=$row['price']?>"></td>
</tr>
<tr>
<td>출판일</td>
<td><input type="text" name="bookdate" value="<?=$row['bookdate']?>"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="imgname" value="<?=$row['imgname']?>"></td>
</tr>
<tr>
<td>책내용</td>
<td><textarea name="desc" id="desc" cols="30" rows="10"><?=$row['desc']?></textarea></td>
</tr>
<tr>
<td colspan="2">
<button>도서수정</button>
<button>취소</button>
</td>
</tr>
</table>
</form>
<script>
let priceInput = document.querySelector('#priceInput');
priceInput.addEventListener('input', function(){
if(isNaN(Number(priceInput.value))){
alert("가격은 숫자만 입력해주세요.");
priceInput.value="";
}
})
</script>
<?php include_once 'include/footer.php' ?>
process_best_create.php
<?php
$file = $_FILES['imgroot'];
$result = move_uploaded_file($file['tmp_name'],'C:Apache24/htdocs/php/book_teach/img/'.$file['name']);
$conn = mysqli_connect('localhost','root','0115','test');
$query = "insert into bestsell(`imgroot`, `imgname`, `writer`, `publisher`, `price`, `bookdate`)
values('../img/{$file['name']}','{$_POST['imgname']}', '{$_POST['writer']}',
'{$_POST['publisher']}','{$_POST['price']}','{$_POST['bookdate']}')";
$result2 = mysqli_query($conn, $query);
echo $query;
if($result2){
echo "성공";
}else {
echo "실패";
}
header('Location:../best.php');
?>
process_best_delete.php
<?php
echo $_POST['id'];
$conn = mysqli_connect('localhost', 'root', '0115', 'test');
$query = "delete from bestsell where id={$_POST['id']}";
$result = mysqli_query($conn, $query);
if($result){
echo "성공";
}else {
echo "실패";
}
header('Location:../best.php');
?>
process_best_edit.php
<?php
//post전송으로 넘어온 데이터는 슈퍼글로벌 $_POST변수가 배열형태로 받는다.
$conn = mysqli_connect('localhost', 'root', '0115', 'test');
$query = "UPDATE bestsell SET `imgname` = '{$_POST['imgname']}', `writer` = '{$_POST['writer']}', `publisher` = '{$_POST['publisher']}', `price` = {$_POST['price']},
`bookdate` = '{$_POST['bookdate']}' WHERE `id` = '{$_GET['id']}'";
echo $query;
$result = mysqli_query($conn, $query);
if($result){
echo "게시글을 작성했습니다.";
}else {
echo "게시글 작성을 실패했습니다.";
}
header('Location:../best.php');
?>



'노력이 좋아서' 카테고리의 다른 글
| <step53>'기프트 샵 만들기' (0) | 2022.06.03 |
|---|---|
| <step52>'베스트 셀러 등록, 수정, 삭제_teach' (0) | 2022.06.02 |
| <step52>'php_이미지 전송' (0) | 2022.06.02 |
| <step51>'과제 실습' (2) | 2022.05.31 |
| <step50>'php_세션,쿠키_실습-도서목록_회원가입, 로그인' (0) | 2022.05.30 |