-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_region.php
More file actions
62 lines (54 loc) · 1.71 KB
/
delete_region.php
File metadata and controls
62 lines (54 loc) · 1.71 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: auth.html");
exit;
}
$user_id = $_SESSION['user_id'];
function alert_redirect($message, $url) {
header('Content-Type: text/html; charset=utf-8');
echo "<script>
alert('" . htmlspecialchars($message, ENT_QUOTES) . "');
location.href = '" . $url . "';
</script>";
exit;
}
function alert_back($message) {
header('Content-Type: text/html; charset=utf-8');
echo "<script>
alert('" . htmlspecialchars($message, ENT_QUOTES) . "');
history.back();
</script>";
exit;
}
// 폼 데이터 받기
// 'region_id' 값이 없으면 뒤로 돌려보냄
if (!isset($_POST['region_id']) || empty($_POST['region_id'])) {
alert_back("삭제할 지역 ID가 없습니다.");
}
$region_id = (int)$_POST['region_id'];
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "team006";
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
alert_back("DB 연결에 실패했습니다: " . $conn->connect_errno);
}
$conn->set_charset("utf8mb4");
// DB에서 해당 지역 "삭제"
// 1. `id`가 일치하고,
// 2. `user_uid`가 현재 로그인한 사용자의 ID와 일치하는 경우에만 삭제
// (다른 사용자의 지역을 삭제하는 것을 방지하기 위함.)
$stmt = $conn->prepare("DELETE FROM user_regions WHERE id = ? AND user_uid = ?");
$stmt->bind_param("is", $region_id, $user_id);
if ($stmt->execute()) {
// 삭제 성공
alert_redirect("선호 지역이 삭제되었습니다.", "dashboard.php");
} else {
// 삭제 실패
alert_back("지역 삭제 중 오류가 발생했습니다: " . $stmt->error);
}
$stmt->close();
$conn->close();
?>