Initial commit - SIG Bansos
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
||||
http_response_code(200);
|
||||
exit();
|
||||
}
|
||||
|
||||
$host = 'sql208.infinityfree.com';
|
||||
$dbname = 'if0_42170546_sig_db';
|
||||
$username = 'if0_42170546';
|
||||
$password = 'SABTU01012005';
|
||||
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
} catch(PDOException $e) {
|
||||
echo json_encode(['success' => false, 'message' => 'Database connection failed']);
|
||||
exit();
|
||||
}
|
||||
|
||||
$action = isset($_GET['action']) ? $_GET['action'] : '';
|
||||
$id = isset($_GET['id']) ? intval($_GET['id']) : null;
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
|
||||
switch ($action) {
|
||||
case 'getAll':
|
||||
$stmt = $pdo->query("SELECT * FROM orang_berkebutuhan ORDER BY id DESC");
|
||||
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
echo json_encode(['success' => true, 'data' => $data]);
|
||||
break;
|
||||
|
||||
case 'getOne':
|
||||
$stmt = $pdo->prepare("SELECT * FROM orang_berkebutuhan WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$data = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if ($data) {
|
||||
echo json_encode(['success' => true, 'data' => $data]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Data tidak ditemukan']);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'create':
|
||||
if ($method === 'POST') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$stmt = $pdo->prepare("INSERT INTO orang_berkebutuhan (nama, alamat, jenis_kebutuhan, no_hp, latitude, longitude, keterangan) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
||||
$stmt->execute([
|
||||
$input['nama'],
|
||||
$input['alamat'] ?? '',
|
||||
$input['jenis_kebutuhan'] ?? '',
|
||||
$input['no_hp'] ?? '',
|
||||
$input['latitude'],
|
||||
$input['longitude'],
|
||||
$input['keterangan'] ?? ''
|
||||
]);
|
||||
echo json_encode(['success' => true, 'message' => 'Data berhasil disimpan', 'id' => $pdo->lastInsertId()]);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
if ($method === 'PUT') {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
$stmt = $pdo->prepare("UPDATE orang_berkebutuhan SET nama=?, alamat=?, jenis_kebutuhan=?, no_hp=?, latitude=?, longitude=?, keterangan=? WHERE id=?");
|
||||
$stmt->execute([
|
||||
$input['nama'],
|
||||
$input['alamat'] ?? '',
|
||||
$input['jenis_kebutuhan'] ?? '',
|
||||
$input['no_hp'] ?? '',
|
||||
$input['latitude'],
|
||||
$input['longitude'],
|
||||
$input['keterangan'] ?? '',
|
||||
$id
|
||||
]);
|
||||
echo json_encode(['success' => true, 'message' => 'Data berhasil diupdate']);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
if ($method === 'DELETE') {
|
||||
$stmt = $pdo->prepare("DELETE FROM orang_berkebutuhan WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
echo json_encode(['success' => true, 'message' => 'Data berhasil dihapus']);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid action']);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user