38960-vm/api/cities.php
2026-03-06 10:01:28 +00:00

37 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/../helpers.php';
header('Content-Type: application/json');
$db = db();
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'POST') {
$input = json_decode(file_get_contents('php://input'), true) ?? $_POST;
$action = $input['action'] ?? '';
if ($action === 'add_city') {
$name_en = $input['name_en'] ?? '';
$name_ar = $input['name_ar'] ?? '';
if ($name_en && $name_ar) {
try {
$stmt = $db->prepare("INSERT INTO cities (name_en, name_ar) VALUES (?, ?)");
$stmt->execute([$name_en, $name_ar]);
$id = $db->lastInsertId();
echo json_encode(['success' => true, 'id' => $id, 'name_en' => $name_en, 'name_ar' => $name_ar]);
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
} else {
echo json_encode(['success' => false, 'error' => 'Missing fields']);
}
} else {
echo json_encode(['success' => false, 'error' => 'Invalid action']);
}
exit;
}
echo json_encode(['success' => false, 'error' => 'Invalid request method']);