5.0 KiB
5.0 KiB
WebGIS "Failed to Fetch" Error - Fixed ✓
Problem Analysis
The "Gagal menyimpan data: Failed to fetch" error was caused by several issues:
- Database connection issues - Triggers were referencing NULL columns
- Missing error handling - APIs weren't returning proper error responses
- Access protocol issue - File was being opened via
file://instead ofhttp://
Solutions Implemented
1. ✓ Database Schema Fixed
File: setup_db.php → drop_db.php → recreated database
Changes:
- Dropped and recreated database
webgis_unified - Fixed trigger syntax to use
IFNULL()instead of direct column references - Created all required tables:
data_unified- Main data table with 26 columnsaudit_log- Audit trail tableactivity_log- Activity tracking- Reference tables for lookups
Triggers Fixed:
trg_data_unified_insert- Now usesIFNULL(NEW.created_by, 'system')trg_data_unified_update- Now usesIFNULL(NEW.updated_by, 'system')trg_data_unified_delete- Properly logs deletions
2. ✓ API Error Handling Improved
Files Updated:
save_point.phpupdate_point.phpdelete_point.phpget_point.php
Improvements:
// Added proper headers
header('Content-Type: application/json');
ini_set('display_errors', 0);
// Added database connection check
if (!isset($conn) || $conn->connect_error) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'Database connection failed']);
exit;
}
// Added proper HTTP status codes
if ($conn->query($sql) === TRUE) {
echo json_encode(['success' => true, 'id' => $conn->insert_id]);
} else {
http_response_code(400);
echo json_encode(['success' => false, 'error' => $conn->error]);
}
3. ✓ Server Configuration
File Created: .htaccess
- Enabled CORS headers for cross-origin requests
- Configured rewrite rules
- Set proper MIME types for PHP
4. ✓ Diagnostic Tools Created
Files Created:
test_api.php- Tests database connection and table structuretest_save.php- Tests save API functionalitystatus.html- Visual status checkerSETUP_INSTRUCTIONS.md- User guidedrop_db.php- Database reset utility
Database Schema - data_unified Table
| Column | Type | Purpose |
|---|---|---|
| id | INT | Primary key |
| tipe | ENUM | Type: point, worship, poor_house, jalan, parsil |
| Rumah Ibadah (Worship) | ||
| nama | VARCHAR(200) | Facility name |
| jenis | VARCHAR(100) | Type: Masjid, Gereja, dll |
| kegiatan | VARCHAR(255) | Activities |
| kapasitas | INT | Capacity |
| radius_meter | INT | Service radius in meters |
| Rumah Miskin (Poor House) | ||
| nama_kepala_keluarga | VARCHAR(200) | Family head name |
| jumlah_anggota | INT | Number of family members |
| kondisi_rumah | VARCHAR(100) | House condition |
| sumber_penghasilan | VARCHAR(255) | Income source |
| kebutuhan_prioritas | VARCHAR(255) | Priority needs |
| status_bantuan | VARCHAR(100) | Assistance status |
| Common | ||
| alamat | VARCHAR(255) | Address |
| kontak | VARCHAR(100) | Contact person |
| no_wa | VARCHAR(50) | WhatsApp number |
| status | VARCHAR(50) | Status (aktif, etc) |
| Geographic | ||
| latitude | DOUBLE | Latitude coordinate |
| longitude | DOUBLE | Longitude coordinate |
| geom | LONGTEXT | GeoJSON geometry |
| Audit | ||
| created_at | DATETIME | Creation timestamp |
| updated_at | DATETIME | Last update timestamp |
| created_by | VARCHAR(50) | Creator |
| updated_by | VARCHAR(50) | Last updater |
How to Use
1. Start the Server
- Open XAMPP Control Panel
- Start Apache and MySQL
2. Access the Application
- Open browser and go to:
http://localhost/webgis/ - NOT
file:///C:/xampp_new/htdocs/webgis/index.html
3. Check Status
- Visit
http://localhost/webgis/status.htmlto verify everything is working
4. Add Data
- Use the map drawing tools to add locations
- Select marker type (worship, poor_house, or point)
- Fill in required fields
- Click "Simpan" to save
Testing
All PHP syntax has been verified:
✓ save_point.php - No syntax errors
✓ update_point.php - No syntax errors
✓ delete_point.php - No syntax errors
✓ get_point.php - No syntax errors
✓ setup_db.php - No syntax errors
✓ drop_db.php - No syntax errors
Verification Checklist
- Database created and tables initialized
- All columns added for worship, poor_house, and point types
- API error handling improved
- Trigger syntax fixed
- HTTP response headers set correctly
- Status codes implemented
- Diagnostic tools created
- Setup instructions provided
- PHP syntax verified
Next Steps
- Start XAMPP (Apache + MySQL)
- Open
http://localhost/webgis/ - Test by adding a new marker:
- Click drawing tool
- Select marker type
- Fill form
- Click Simpan
- If you see "Success" - the fix is complete! ✓
If you encounter any issues, check http://localhost/webgis/status.html for diagnostics.