https://hyonyong.tistory.com/entry/PHP-book1
[PHP] book1
index.php
hyonyong.tistory.com
member > join.php
<?php
include_once '../include/header.php'
?>
<!-- 💚 회원가입 -->
<div id="write_book" class="inner">
<h2>회원가입</h2>
<h3>회원정보를 입력하세요.</h3>
<form action="../process/join_process.php" method="post">
<table>
<tr>
<td>아이디</td>
<td><input type="text" name="userId" required></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="userPw" required></td>
</tr>
<tr>
<td>비밀번호 체크</td>
<td><input type="password" name="userPwCh" required></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="userName" required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">회원가입</button>
<button type="reset">취소</button>
</td>
</tr>
</table>
</form>
</div>
<?php
include_once '../include/footer.php'
?>
member > login.php
<?php
include_once '../include/header.php'
?>
<!-- 💚 로그인 -->
<div id="write_book" class="inner">
<h2>로그인</h2>
<h3>아이디와 패스워드를 입력하세요.</h3>
<form action="../process/login_process.php" method="post">
<table>
<tr>
<td>아이디</td>
<td><input type="text" name="userId" required></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="userPw" required></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">로그인</button>
<button type="reset">취소</button>
<button type="button" onclick="location.href='join.php'">회원가입</button>
</td>
</tr>
</table>
</form>
</div>
<?php
include_once '../include/footer.php'
?>
process > create_process.php
<?php
// 💚 도서등록
// method="post" 전송으로 넘어온 데이터는
// 슈퍼글로벌 $_POST 변수가 배열형태로 받는다.
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
$query = "insert into books(`writer`, `title`, `publisher`, `price`, `bookdate`, `desc`)
values('{$_POST['writer']}', '{$_POST['title']}', '{$_POST['publisher']}',
{$_POST['price']}, '{$_POST['bookdate']}', '{$_POST['desc']}')";
// 숫자는 '' => 감싸지 않음
echo $query; // 잘 작동하는지 확인
// insert into books(`writer`, `title`, `pucblisher`, `price`, `bookdate`, `desc`)
// values('글쓴이', '제목', '출판사', 123, '출판일', '책 내용')게시글을 작성했습니다.
$result = mysqli_query($conn, $query);
if($result) {
echo "게시글을 작성했습니다. ⭕";
} else {
echo "게시글 작성 실패했습니다. ❌";
}
// 메인으로 넘기기
header('Location:../index.php');
?>
process > delete_process.php
<!-- 💚 삭제 -->
<?php
var_dump($_POST);
echo $_POST['id']; // 확인용, 삭제 누르면 값에 접근해서 id 가 출력됨
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
$query = "delete from books where id={$_POST['id']}";
$result = mysqli_query($conn, $query);
if($result) { // 확인용
echo "삭제 성공 ⭕";
} else {
echo "삭제 실패 ❌";
}
header('Location:../index.php');
?>
process > edit_process.php
<?php
// 💚 도서수정
// var_dump($_POST); // 확인용
// POST 전송으로 전송된 데이터는 $_POST가 가지고있음
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
// 데이터 수정은 update문 사용
// update 테이블이름
// set 컬럼명 = "변경될 값"
// where 컬럼
$query = "update books set
`writer` = '{$_POST['writer']}',
`title` = '{$_POST['title']}',
`publisher` = '{$_POST['publisher']}',
`price` = '{$_POST['price']}',
`bookdate` = '{$_POST['bookdate']}',
`desc` = '{$_POST['desc']}'
where `id` = '{$_POST['id']}'
";
echo $query; // 확인용
if($result) {echo "성공";}
else {echo "실패";}
$result = mysqli_query($conn, $query);
header('Location:../index.php');
?>
process > gallery_create_process.php
<?php
// 💚 베스트셀러등록
// var_dump($_FILES);
// var_dump($_POST);
// 변수에 저장하기
// $file_tmp = $_FILES['img']['tmp_name'];
$fileimg = $_FILES['img'];
// move_uploaded_file(현재위치, 업로드할 위치);
move_uploaded_file($fileimg['tmp_name'],
"C:\Apache24\htdocs\php\book\images/".$fileimg['name']);
// create_process 복붙해오기
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
$query = "insert into galleryBoard(`writer`, `title`, `publisher`, `price`, `bookdate`, `desc`, `imgsrc`)
values('{$_POST['writer']}', '{$_POST['title']}', '{$_POST['publisher']}',
{$_POST['price']}, '{$_POST['bookdate']}', '{$_POST['desc']}', '{$fileimg['name']}')";
echo $query;
$result = mysqli_query($conn, $query);
if($result) {
echo "게시글을 작성했습니다. ⭕";
} else {
echo "게시글 작성 실패했습니다. ❌";
}
header('Location:../gallery_board.php');
?>
process > gallery_delete_process.php
<?php
// 💚 목록(board) → 베스트셀러 자세히 보기 → 삭제
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
$query = "delete from galleryBoard where id={$_POST['id']}";
$result = mysqli_query($conn, $query);
// 🧡 (1) 쿼리문 2개 작성하기
// $result2 = mysqli_query($conn, "select from galleryBoard where id={$_POST['id']}");
// $row = mysqli_fetch_array($result2);
if($result) {
// 💚 파일 삭제 (images 안에 img)
// unlink("/php/book/images/".$row['imgsrc']);
// 절대 경로 X, 상대경로로 넣어주기
unlink("../images/".$_POST['imgsrc']);
echo "삭제 완료.";
} else {
echo "실패";
}
// header('Location:../gallery_board.php');
?>
process > gallery_edit_process.php
<?php
// 💚 베스트셀러 도서 수정
// 잘 받아오는지 확인
// var_dump($_FILES);
// var_dump($_POST);
// (1)바뀔수도 (2)안바뀔수도 있으니 변수 지정 , (3)img사용안하고싶을수도있음
// img 안에 name
$img_src = $_FILES ? $_FILES['img']['name'] : $_POST['old_img'];
// name이 일치하는지 비교
if($_FILES['img']['name'] != $_POST['old_img']) { // 다를경우
// 책 표지가 변경되면 이전 파일을 삭제
// 변경된 파일을 이미지 폴더로 업로드
unlink('../images/'.$_POST['old_img']); // 삭제
move_uploaded_file($_FILES['img']['tmp_name'], // tmp_name 임시저장소
'C:/Apache24/htdocs/php/book/images/'.$_FILES['img']['name']);
}
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
$query = "update galleryBoard set
`writer` = '{$_POST['writer']}',
`imgsrc` = '{$img_src}',
`title` = '{$_POST['title']}',
`publisher` = '{$_POST['publisher']}',
`price` = {$_POST['price']},
`bookdate` = '{$_POST['bookdate']}',
`desc` = '{$_POST['desc']}'
where `id` = {$_POST['id']}
";
$result = mysqli_query($conn, $query);
if($result) {
echo "성공 ⭕";
} else {
echo "실패 ❌";
echo $query;
}
header('Location:../gallery_board.php');
?>
process > join_process.php
<?php
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
$query = "insert into members (id, pw, date, name)
values(
'{$_POST['userId']}',
'{$_POST['userPw']}',
NOW(),
'{$_POST['userName']}')";
$result = mysqli_query($conn, $query);
if($result) {
echo "성공";
} else {
echo "실패";
}
header('Location:../index.php');
?>
process > login_process.php
<?php
session_start();
// member 테이블에 등록된 회원인지 확인 → select문
$conn = mysqli_connect('localhost', 'root', '1234', 'test');
$query = "select * from members where id='{$_POST['userId']}'";
$result = mysqli_query($conn, $query);
// (1) 아이디가 있다면 비밀번호도 맞는지 검사
// 몇개의 행이 있는지 확인, 아아디 1개
if(mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_array($result);
// (2) 비밀번호 확인 → 맞으면 세션 생성
if($_POST['userPw'] == $row['pw']) { // pw 데이터베이스 안에 있는 값
$_SESSION['userId'] = $_POST['userId'];
// 세션 아이디가 있으면 "로그인 되었습니다." 경고창 출력
if(isset($_SESSION['userId'])) {
?>
<script>
alert("로그인 되었습니다.");
location.replace("../index.php");
</script>
<?php
}
} else {
?>
<script>
alert("비밀번호가 맞지 않습니다.");
location.replace("../index.php");
</script>
<?php
}
} else {
?>
<script>
alert("아이디가 맞지 않습니다.");
location.replace("../index.php");
</script>
<?php
}
?>
process > logout_process.php
<?php
session_start();
$result = session_destroy();
if($result) {
?>
<script>
alert ("로그아웃 되었습니다.");
// (1)
// location.replace('../index.php');
// (2) 이전 페이지로 이동
history.back();
</script>
<?php
}
?>
'PHP' 카테고리의 다른 글
[PHP] Global Variables - Superglobals (0) | 2023.05.04 |
---|---|
[PHP] Strings, Math, 상수, 연산자, 반복문, 조건문 (0) | 2023.05.04 |
[PHP] book1 (0) | 2023.04.05 |
[PHP] blog (0) | 2023.04.05 |
[PHP] file (0) | 2023.04.05 |