PHP 도서관리 #1
book 테이블 생성
1 2 3 4 5 6 7 |
CREATE TABLE book ( no integer, title varchar(50) NOT NULL, pric integer, publ varchar(50) NOT NULL, PRIMARY KEY(no) ) |
/db/dbconn.php
1 2 3 |
<?php $connect = mysqli_connect("127.0.0.1", "아이디", "암호", "데이터베이스명"); ?> |
/book/book_register_form.php
1 2 3 4 5 6 7 8 9 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 홍길동의 도서 저장<br> <form action="book_register.php" method="GET"> 도서명 : <input type="text" name="title"><br> 가 격 : <input type="text" name="pric"><br> 출판사 : <input type="text" name="publ"><br> <input type="submit" value="저장"> <input type="reset" value="취소"> </form> |
/book/book_register.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php header("Content-Type: text/html; charset=UTF-8"); $title = $_GET["title"]; $pric = $_GET["pric"]; $publ = $_GET["publ"]; $sql = "INSERT INTO book(title, pric, publ)"; $sql .= " VALUES ('$title', $pric, '$publ');"; //echo $sql; include "../db/dbconn.php"; $result = mysqli_query($connect, $sql); mysqli_close($connect); echo $result . "개 입력되었습니다"; ?> |
/book/book_list.php 버전1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php include "../db/dbconn.php"; $sql = "SELECT * FROM book;"; $result = mysqli_query($connect, $sql); while($row = mysqli_fetch_array($result)){ echo $row['no'] . " "; echo $row['title'] . " "; echo "".$row['pric'] . " "; echo "".$row['publ'] . "<br>"; } mysqli_close($connect); ?> <a href='./book_register_form.php'>글쓰기</a> |
/book/book_list.php 버전2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<style> body{ background-color:#000; color:#fff; } div { color:#f00; font-size:30px; } </style> <?php include "../db/dbconn.php"; $sql = "SELECT * FROM book;"; $result = mysqli_query($connect, $sql); while($row = mysqli_fetch_array($result)){ echo $row['no'] . " "; echo "<a href='./book_detail.php?no=".$row['no']."'>"; echo $row['title'] . " "; echo "</a>"; echo "".$row['pric'] . " "; echo "".$row['publ'] . "<br>"; } mysqli_close($connect); ?> <a href='./book_register_form.php'>글쓰기</a> |
/book/book_detail.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $no = $_GET["no"]; include "../db/dbconn.php"; $sql = "SELECT * FROM book WHERE no=$no"; $result = mysqli_query($connect, $sql); if($row = mysqli_fetch_array($result)){ echo $row['no']; echo $row['title']; echo $row['pric']; echo $row['publ']; } mysqli_close($connect); ?> <a href="book_delete.php?no=<?=$no?>">삭제</a> <a href="book_update_form.php?no=<?=$no?>">수정</a> |
/book/book_delete.php
1 2 3 4 5 6 7 8 9 10 11 |
<?php $no = $_GET["no"]; include "../db/dbconn.php"; $sql = "DELETE FROM book WHERE no=$no"; $result = mysqli_query($connect, $sql); mysqli_close($connect); header('Location: ./book_list.php'); ?> |
/book/book_update_form.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php $no = $_GET["no"]; include "../db/dbconn.php"; $sql = "SELECT * FROM book WHERE no='$no';"; $result = mysqli_query($connect, $sql); if($row = mysqli_fetch_array($result)){ $title = $row['title']; $pric = $row['pric']; $publ = $row['publ']; } mysqli_close($connect); ?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 홍길동의 도서 수정<br> <form action="book_update.php" method="GET"> <input type="hidden" name="no" value="<?=$no?>"> 도서명 : <input type="text" name="title" value="<?=$title?>"><br> 가 격 : <input type="text" name="pric" value="<?=$pric?>"><br> 출판사 : <input type="text" name="publ" value="<?=$publ?>"><br> <input type="submit" value="수정"> <input type="reset" value="취소"> </form> |
/book/book_update.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $no = $_GET['no']; $title = $_GET['title']; $pric = $_GET['pric']; $publ = $_GET['publ']; include "../db/dbconn.php"; $sql = "UPDATE book SET title='$title' "; $sql .= ", pric='$pric'"; $sql .= ", publ='$publ'"; $sql .= " WHERE no='$no'"; //echo $sql; $result = mysqli_query($connect, $sql); mysqli_close($connect); header('Location: ./book_list.php'); ?> |