diff --git a/public/userarea/quotations.php b/public/userarea/quotations.php index 604c311..96a6d52 100644 --- a/public/userarea/quotations.php +++ b/public/userarea/quotations.php @@ -25,15 +25,21 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' $description = ''; $customer = ''; - $stmt = $pdo->prepare("INSERT INTO quotations (description, customer, iduser) VALUES (?, ?, ?)"); - $stmt->execute([$description, $customer, $user_id]); - $newId = $pdo->lastInsertId(); - - // Log creazione - error_log("Creata nuova quotation ID: $newId"); - - // Reindirizza alla modifica della nuova quotation - header("Location: quotations.php?edit_id=" . $newId . "&status=success&message=" . urlencode("Quotation creata con successo")); + try { + $stmt = $pdo->prepare("INSERT INTO quotations (description, customer, iduser) VALUES (?, ?, ?)"); + $success = $stmt->execute([$description, $customer, $user_id]); + if ($success) { + $newId = $pdo->lastInsertId(); + error_log("Creata nuova quotation ID: $newId"); + header("Location: quotations.php?edit_id=" . $newId . "&status=success&message=" . urlencode("Quotation creata con successo")); + } else { + error_log("Errore: Impossibile creare la quotation, nessun ID generato."); + header("Location: quotations.php?status=error&message=" . urlencode("Errore durante la creazione della quotation")); + } + } catch (PDOException $e) { + error_log("Errore PDO durante la creazione della quotation: " . $e->getMessage()); + header("Location: quotations.php?status=error&message=" . urlencode("Errore database: " . $e->getMessage())); + } exit; } @@ -43,14 +49,15 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' $description = $_POST['description'] ?? ''; $customer = $_POST['customer'] ?? ''; - $stmt = $pdo->prepare("UPDATE quotations SET description = ?, customer = ? WHERE id = ? AND iduser = ?"); - $stmt->execute([$description, $customer, $id, $user_id]); - - // Log modifica - error_log("Modificata quotation ID: $id"); - - // Reindirizza alla lista delle quotations - header("Location: quotations.php?status=success&message=" . urlencode("Quotation modificata con successo")); + try { + $stmt = $pdo->prepare("UPDATE quotations SET description = ?, customer = ? WHERE id = ? AND iduser = ?"); + $stmt->execute([$description, $customer, $id, $user_id]); + error_log("Modificata quotation ID: $id"); + header("Location: quotations.php?status=success&message=" . urlencode("Quotation modificata con successo")); + } catch (PDOException $e) { + error_log("Errore PDO durante la modifica della quotation: " . $e->getMessage()); + header("Location: quotations.php?status=error&message=" . urlencode("Errore database: " . $e->getMessage())); + } exit; } @@ -58,28 +65,43 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST[' if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['id'])) { $id = intval($_POST['id']); - $stmt = $pdo->prepare("DELETE FROM quotations WHERE id = ? AND iduser = ?"); - $stmt->execute([$id, $user_id]); - - // Log cancellazione - error_log("Cancellata quotation ID: $id"); - - header("Location: quotations.php?status=success&message=" . urlencode("Quotation cancellata con successo")); + try { + $stmt = $pdo->prepare("DELETE FROM quotations WHERE id = ? AND iduser = ?"); + $stmt->execute([$id, $user_id]); + error_log("Cancellata quotation ID: $id"); + header("Location: quotations.php?status=success&message=" . urlencode("Quotation cancellata con successo")); + } catch (PDOException $e) { + error_log("Errore PDO durante la cancellazione della quotation: " . $e->getMessage()); + header("Location: quotations.php?status=error&message=" . urlencode("Errore database: " . $e->getMessage())); + } exit; } // Recupera tutte le quotations per l'utente -$stmt = $pdo->prepare("SELECT * FROM quotations WHERE iduser = ? ORDER BY creation_date DESC"); -$stmt->execute([$user_id]); -$quotations = $stmt->fetchAll(PDO::FETCH_ASSOC); +try { + $stmt = $pdo->prepare("SELECT * FROM quotations WHERE iduser = ? ORDER BY creation_date DESC"); + $stmt->execute([$user_id]); + $quotations = $stmt->fetchAll(PDO::FETCH_ASSOC); +} catch (PDOException $e) { + error_log("Errore PDO durante il recupero delle quotations: " . $e->getMessage()); + $quotations = []; +} // Verifica se è richiesta la modifica di una quotation $editQuotation = null; if (isset($_GET['edit_id'])) { $editId = intval($_GET['edit_id']); - $stmt = $pdo->prepare("SELECT * FROM quotations WHERE id = ? AND iduser = ?"); - $stmt->execute([$editId, $user_id]); - $editQuotation = $stmt->fetch(PDO::FETCH_ASSOC); + try { + $stmt = $pdo->prepare("SELECT * FROM quotations WHERE id = ? AND iduser = ?"); + $stmt->execute([$editId, $user_id]); + $editQuotation = $stmt->fetch(PDO::FETCH_ASSOC); + if (!$editQuotation) { + error_log("Nessuna quotation trovata per id: $editId"); + } + } catch (PDOException $e) { + error_log("Errore PDO durante il recupero della quotation per modifica: " . $e->getMessage()); + $editQuotation = null; + } } ?> @@ -95,7 +117,6 @@ if (isset($_GET['edit_id'])) {