getConnection(); if (!isset($iduserlogin)) { die("Errore: Utente non loggato."); } // Recupera dati da auth_users $stmt = $pdo->prepare("SELECT first_name, last_name, email FROM auth_users WHERE id = ?"); $stmt->execute([$iduserlogin]); $user = $stmt->fetch(); if (!$user) { die("Utente non trovato."); } // Recupera (o crea) record studente $stmt = $pdo->prepare("SELECT * FROM students WHERE user_id = ?"); $stmt->execute([$iduserlogin]); $student = $stmt->fetch(); $is_new = !$student; $success_message = $error = ''; // === GESTIONE SALVATAGGIO === if ($_SERVER['REQUEST_METHOD'] === 'POST') { $codice_fiscale = trim($_POST['codice_fiscale'] ?? ''); $partita_iva = trim($_POST['partita_iva'] ?? ''); $company_name = trim($_POST['company_name'] ?? ''); $billing_address = trim($_POST['billing_address']); $billing_postal_code = trim($_POST['billing_postal_code']); $billing_city = trim($_POST['billing_city']); $billing_province = strtoupper(trim($_POST['billing_province'])); $billing_country = trim($_POST['billing_country'] ?: 'Italia'); $same_shipping = !empty($_POST['shipping_same_as_billing']); $shipping_address = $same_shipping ? $billing_address : trim($_POST['shipping_address'] ?? ''); $shipping_postal_code = $same_shipping ? $billing_postal_code : trim($_POST['shipping_postal_code'] ?? ''); $shipping_city = $same_shipping ? $billing_city : trim($_POST['shipping_city'] ?? ''); $shipping_province = $same_shipping ? $billing_province : strtoupper(trim($_POST['shipping_province'] ?? '')); $shipping_country = $same_shipping ? $billing_country : trim($_POST['shipping_country'] ?? 'Italia'); $emergency_name = trim($_POST['emergency_contact_name'] ?? ''); $emergency_phone = trim($_POST['emergency_contact_phone'] ?? ''); $medical_notes = trim($_POST['medical_notes'] ?? ''); $privacy_consent = !empty($_POST['privacy_consent']); $marketing_consent = !empty($_POST['marketing_consent']); // Validazione minima if (empty($billing_address) || empty($billing_city) || empty($billing_postal_code) || empty($billing_province)) { $error = "Compila tutti i campi obbligatori dell'indirizzo di fatturazione."; } elseif (!$privacy_consent) { $error = "Devi accettare l'informativa privacy per continuare."; } else { try { if ($is_new) { $stmt = $pdo->prepare(" INSERT INTO students ( user_id, codice_fiscale, partita_iva, company_name, billing_address, billing_postal_code, billing_city, billing_province, billing_country, shipping_same_as_billing, shipping_address, shipping_postal_code, shipping_city, shipping_province, shipping_country, emergency_contact_name, emergency_contact_phone, medical_notes, privacy_consent, marketing_consent ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) "); $stmt->execute([ $iduserlogin, $codice_fiscale, $partita_iva, $company_name, $billing_address, $billing_postal_code, $billing_city, $billing_province, $billing_country, $same_shipping ? 1 : 0, $shipping_address, $shipping_postal_code, $shipping_city, $shipping_province, $shipping_country, $emergency_name, $emergency_phone, $medical_notes, $privacy_consent ? 1 : 0, $marketing_consent ? 1 : 0 ]); $success_message = "Profilo completato con successo! Benvenuto"; } else { $stmt = $pdo->prepare(" UPDATE students SET codice_fiscale = ?, partita_iva = ?, company_name = ?, billing_address = ?, billing_postal_code = ?, billing_city = ?, billing_province = ?, billing_country = ?, shipping_same_as_billing = ?, shipping_address = ?, shipping_postal_code = ?, shipping_city = ?, shipping_province = ?, shipping_country = ?, emergency_contact_name = ?, emergency_contact_phone = ?, medical_notes = ?, privacy_consent = ?, marketing_consent = ? WHERE user_id = ? "); $stmt->execute([ $codice_fiscale, $partita_iva, $company_name, $billing_address, $billing_postal_code, $billing_city, $billing_province, $billing_country, $same_shipping ? 1 : 0, $shipping_address, $shipping_postal_code, $shipping_city, $shipping_province, $shipping_country, $emergency_name, $emergency_phone, $medical_notes, $privacy_consent ? 1 : 0, $marketing_consent ? 1 : 0, $iduserlogin ]); $success_message = "Dati aggiornati con successo!"; } // Ricarica i dati aggiornati $stmt = $pdo->prepare("SELECT * FROM students WHERE user_id = ?"); $stmt->execute([$iduserlogin]); $student = $stmt->fetch(); $is_new = false; } catch (Exception $e) { $error = "Errore del database: " . $e->getMessage(); } } } ?>