# 🔧 Quick Fix - SQL Parameter Error

## Error yang Terjadi
```
SQLSTATE[HY093]: Invalid parameter number
```

## Penyebab
PDO tidak bisa menggunakan named parameter yang sama lebih dari sekali dalam satu query. Error terjadi di `ON DUPLICATE KEY UPDATE` karena parameter seperti `:setting_value` digunakan 2x (di VALUES dan UPDATE).

---

## ✅ Solusi Cepat

### Opsi 1: Download File yang Sudah Diperbaiki
File yang sudah diperbaiki:
- `install_voucher_settings.php`
- `agent-admin/voucher_settings.php`

**Langkah:**
1. Backup file lama
2. Replace dengan file baru
3. Test kembali

---

### Opsi 2: Manual Fix

#### File 1: `install_voucher_settings.php`

**Cari baris ini (sekitar line 78-95):**
```php
$stmt = $db->prepare("
    INSERT INTO agent_settings (setting_key, setting_value, setting_type, description, updated_by) 
    VALUES (:key, :value, :type, :desc, 'system')
    ...
");

$stmt->execute([
    ':key' => $setting[0],
    ':value' => $setting[1],
    ':type' => $setting[2],
    ':desc' => $setting[3]
]);
```

**Ganti dengan (v1.0.2 - FINAL FIX):**
```php
foreach ($settings as $setting) {
    // Check if exists
    $checkStmt = $db->prepare("SELECT setting_key FROM agent_settings WHERE setting_key = ?");
    $checkStmt->execute([$setting[0]]);
    $exists = $checkStmt->rowCount() > 0;
    
    // Prepare inside loop with positional parameters
    $stmt = $db->prepare("
        INSERT INTO agent_settings (setting_key, setting_value, setting_type, description, updated_by) 
        VALUES (?, ?, ?, ?, 'system')
        ON DUPLICATE KEY UPDATE 
            setting_value = ?,
            setting_type = ?,
            description = ?
    ");
    
    $stmt->execute([
        $setting[0], // setting_key
        $setting[1], // setting_value
        $setting[2], // setting_type
        $setting[3], // description
        $setting[1], // setting_value for UPDATE
        $setting[2], // setting_type for UPDATE
        $setting[3]  // description for UPDATE
    ]);
}
```

---

#### File 2: `agent-admin/voucher_settings.php`

**Cari baris ini (sekitar line 55-62):**
```php
$stmt = $db->prepare("
    INSERT INTO agent_settings (setting_key, setting_value, setting_type, description, updated_by) 
    VALUES (:key, :value, 'string', 'Voucher generation setting', 'admin')
    ON DUPLICATE KEY UPDATE setting_value = :value, updated_by = 'admin'
");
$stmt->execute([':key' => $key, ':value' => $value]);
```

**Ganti dengan (v1.0.2 - FINAL FIX):**
```php
foreach ($settingsToSave as $key => $value) {
    $stmt = $db->prepare("
        INSERT INTO agent_settings (setting_key, setting_value, setting_type, description, updated_by) 
        VALUES (?, ?, 'string', 'Voucher generation setting', 'admin')
        ON DUPLICATE KEY UPDATE setting_value = ?, updated_by = 'admin'
    ");
    $stmt->execute([$key, $value, $value]);
}
```

---

## 🧪 Testing

Setelah fix, test dengan:

1. **Jalankan installer:**
   ```
   http://your-domain/mikhmon/install_voucher_settings.php
   ```
   Harus berhasil tanpa error

2. **Test save settings:**
   - Login admin
   - Buka Format Voucher
   - Ubah setting
   - Klik Simpan
   - Harus muncul "Pengaturan berhasil disimpan"

3. **Test generate voucher:**
   - Login sebagai agent
   - Generate voucher
   - Harus berhasil dengan format sesuai setting

---

## ✅ Verifikasi Fix Berhasil

Cek di database:
```sql
SELECT * FROM agent_settings WHERE setting_key LIKE 'voucher_%';
```

Harus return 8 rows:
- voucher_username_password_same
- voucher_username_type
- voucher_username_length
- voucher_password_type
- voucher_password_length
- voucher_prefix_enabled
- voucher_prefix
- voucher_uppercase

---

## 📞 Masih Error?

1. Clear browser cache (Ctrl+F5)
2. Cek PHP error log
3. Cek MySQL error log
4. Pastikan file sudah di-save dengan benar
5. Restart web server (jika perlu)

---

**Status:** ✅ FIXED in v1.0.2 (Final)  
**Severity:** Critical  
**Impact:** Installation & Configuration  
**Solution:** Use positional parameters (?) instead of named parameters (:param)
