- URL 하드코딩 → .env APP_URL 기반 동적 URL로 변경 - DB 연결 하드코딩 → .env 기반으로 변경 - MySQL strict mode DATE 오류 수정
75 lines
2.7 KiB
PHP
75 lines
2.7 KiB
PHP
<?php
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . "/session.php");
|
|
|
|
if (!isset($_SESSION["level"]) || $_SESSION["level"] > 5) {
|
|
http_response_code(403);
|
|
echo json_encode(['success' => false, 'message' => '권한이 없습니다.']);
|
|
exit;
|
|
}
|
|
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . "/lib/mydb.php");
|
|
$pdo = db_connect();
|
|
|
|
$response = ['success' => false, 'message' => ''];
|
|
|
|
try {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$vehicle_number = $_POST['vehicle_number'] ?? '';
|
|
$initial_distance = $_POST['initial_distance'] ?? 0;
|
|
|
|
if (empty($vehicle_number)) {
|
|
throw new Exception('차량번호가 필요합니다.');
|
|
}
|
|
|
|
// 숫자 검증
|
|
if (!is_numeric($initial_distance) || $initial_distance < 0) {
|
|
throw new Exception('유효한 주행거리를 입력해주세요.');
|
|
}
|
|
|
|
$car_table = 'juilcar';
|
|
|
|
// initial_distance 컬럼이 있는지 확인하고 없으면 추가
|
|
$check_column_sql = "SHOW COLUMNS FROM " . $DB . "." . $car_table . " LIKE 'initial_distance'";
|
|
$check_stmh = $pdo->query($check_column_sql);
|
|
$column_exists = $check_stmh->rowCount() > 0;
|
|
|
|
if (!$column_exists) {
|
|
// initial_distance 컬럼 추가
|
|
$add_column_sql = "ALTER TABLE " . $DB . "." . $car_table . " ADD COLUMN initial_distance DECIMAL(10,2) DEFAULT 0.00 COMMENT '최초 주행거리'";
|
|
$pdo->exec($add_column_sql);
|
|
}
|
|
|
|
// 최초 주행거리 업데이트
|
|
$sql = "UPDATE " . $DB . "." . $car_table . "
|
|
SET initial_distance = :initial_distance
|
|
WHERE vehicle_number = :vehicle_number AND is_deleted IS NULL";
|
|
|
|
$stmh = $pdo->prepare($sql);
|
|
$stmh->bindValue(':initial_distance', $initial_distance, PDO::PARAM_STR);
|
|
$stmh->bindValue(':vehicle_number', $vehicle_number, PDO::PARAM_STR);
|
|
|
|
$result = $stmh->execute();
|
|
|
|
if ($result) {
|
|
$response = [
|
|
'success' => true,
|
|
'message' => '최초 주행거리가 성공적으로 저장되었습니다.',
|
|
'vehicle_number' => $vehicle_number,
|
|
'initial_distance' => $initial_distance
|
|
];
|
|
} else {
|
|
throw new Exception('저장 중 오류가 발생했습니다.');
|
|
}
|
|
} else {
|
|
throw new Exception('잘못된 요청입니다.');
|
|
}
|
|
} catch (Exception $e) {
|
|
$response = [
|
|
'success' => false,
|
|
'message' => $e->getMessage()
|
|
];
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($response, JSON_UNESCAPED_UNICODE);
|
|
?>
|