65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
// Enable errors for debugging
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
ini_set('log_errors', 1);
|
|
ini_set('error_log', __DIR__ . '/delete_record_debug.log');
|
|
|
|
// Log iniziale
|
|
error_log("Inizio cancellazione record alle " . date('Y-m-d H:i:s'));
|
|
|
|
// Includi il file di configurazione del database
|
|
include('include/headscript.php');
|
|
|
|
// Ricevi l'ID dalla richiesta POST
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$iddatadb = isset($input['id']) ? intval($input['id']) : 0;
|
|
|
|
if ($iddatadb <= 0) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'message' => 'ID non valido']);
|
|
exit;
|
|
}
|
|
|
|
// Connessione al database
|
|
try {
|
|
$db = DBHandlerSelect::getInstance();
|
|
$pdo = $db->getConnection();
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => 'Errore di connessione al database: ' . $e->getMessage()]);
|
|
error_log("Errore di connessione al database: " . $e->getMessage());
|
|
exit;
|
|
}
|
|
|
|
// Inizia una transazione
|
|
$pdo->beginTransaction();
|
|
|
|
try {
|
|
// Elimina i dettagli associati dal tavolo import_data_details
|
|
$stmt = $pdo->prepare("DELETE FROM import_data_details WHERE id = ?");
|
|
$stmt->execute([$iddatadb]);
|
|
|
|
// Elimina il record principale dal tavolo datadb
|
|
$stmt = $pdo->prepare("DELETE FROM datadb WHERE iddatadb = ?");
|
|
$stmt->execute([$iddatadb]);
|
|
|
|
// Verifica se il record è stato eliminato
|
|
if ($stmt->rowCount() > 0) {
|
|
$pdo->commit();
|
|
echo json_encode(['success' => true, 'message' => 'Record eliminato con successo']);
|
|
error_log("Record con iddatadb=$iddatadb eliminato con successo");
|
|
} else {
|
|
$pdo->rollBack();
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'message' => 'Record non trovato']);
|
|
error_log("Record con iddatadb=$iddatadb non trovato");
|
|
}
|
|
} catch (Exception $e) {
|
|
$pdo->rollBack();
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'message' => 'Errore durante la cancellazione: ' . $e->getMessage()]);
|
|
error_log("Errore durante la cancellazione del record con iddatadb=$iddatadb: " . $e->getMessage());
|
|
}
|