-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_region.php
More file actions
99 lines (84 loc) · 2.86 KB
/
add_region.php
File metadata and controls
99 lines (84 loc) · 2.86 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
// 세션 시작 및 로그인 확인
session_start();
if (!isset($_SESSION['user_id'])) {
// 1-1.로그인 하지 않았을 시 로그인화면으로 다시 이동
header("Location: auth.html");
exit;
}
$user_id = $_SESSION['user_id'];
// 1-2.알림창/이동 함수
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;
}
// 폼 데이터 받기
if (!isset($_POST['step1']) || empty($_POST['step1'])) {
alert_back("시/도를 선택해주세요.");
}
if (!isset($_POST['step2']) || empty($_POST['step2'])) {
alert_back("시/군/구를 선택해주세요.");
}
if (!isset($_POST['step3']) || empty($_POST['step3'])) {
alert_back("동/읍/면을 선택해주세요.");
}
$step1 = $_POST['step1'];
$step2 = $_POST['step2'];
$step3 = $_POST['step3'];
// 지역명 생성
$region_name = trim("$step1 $step2 $step3");
$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");
// 중복 저장 방지
$stmt = $conn->prepare("SELECT id FROM user_regions WHERE user_uid = ? AND region_name = ?");
$stmt->bind_param("ss", $user_id, $region_name);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->close();
alert_back("'" . htmlspecialchars($region_name) . "' 지역은 이미 선호 지역에 추가되어 있습니다.");
}
$stmt->close();
// 6. grid_x, grid_y 조회 (region_dictionary)
$stmt = $conn->prepare("SELECT grid_x, grid_y, stnId FROM region_dictionary WHERE step1 = ? AND step2 = ? AND step3 = ? LIMIT 1");
$stmt->bind_param("sss", $step1, $step2, $step3);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->close();
if (!$row) {
alert_back("선택한 지역의 날씨 좌표 정보를 찾을 수 없습니다.");
}
$region_nx = (int)$row['grid_x'];
$region_ny = (int)$row['grid_y'];
$stnId = $row['stnId'];
// DB에 새 지역 "추가"
$stmt = $conn->prepare("INSERT INTO user_regions (user_uid, region_name, region_nx, region_ny, stnId) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("ssiii", $user_id, $region_name, $region_nx, $region_ny, $stnId);
if ($stmt->execute()) {
alert_redirect($region_name . " 지역이 추가되었습니다.", "dashboard.php");
} else {
alert_back("지역 추가 중 오류가 발생했습니다: " . $stmt->error);
}
$stmt->close();
$conn->close();
?>