prepare(" SELECT ps.*, p.project_name FROM project_selections ps INNER JOIN projects p ON ps.project_id = p.id WHERE ps.id = ? "); $stmt->execute([$selection_id]); $selection = $stmt->fetch(); if (!$selection) { header('Location: panel.php'); exit; } $project_id = $selection['project_id']; // Load existing URLs $stmt = $pdo->prepare("SELECT * FROM selection_survey_urls WHERE selection_id = ?"); $stmt->execute([$selection_id]); $existing_urls = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); } else { // Get project details $stmt = $pdo->prepare("SELECT * FROM projects WHERE id = ?"); $stmt->execute([$project_id]); $project = $stmt->fetch(); if (!$project) { header('Location: panel.php'); exit; } // Get selections for this project $stmt = $pdo->prepare("SELECT id, selection_name FROM project_selections WHERE project_id = ? ORDER BY created_at DESC"); $stmt->execute([$project_id]); $selections = $stmt->fetchAll(); } } catch (Exception $e) { error_log("Survey URLs error: " . $e->getMessage()); die("Error loading page"); } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $pdo->beginTransaction(); $target_selection_id = (int)$_POST['selection_id']; $urls = $_POST['url'] ?? []; // Delete existing URLs for this selection $stmt = $pdo->prepare("DELETE FROM selection_survey_urls WHERE selection_id = ?"); $stmt->execute([$target_selection_id]); // Insert new URLs $stmt = $pdo->prepare(" INSERT INTO selection_survey_urls (selection_id, url_type, url) VALUES (?, ?, ?) "); foreach ($urls as $type => $url) { if (!empty(trim($url))) { $stmt->execute([$target_selection_id, $type, trim($url)]); } } // Log activity $stmt = $pdo->prepare(" INSERT INTO selection_activity_log (selection_id, action, description, performed_by) VALUES (?, ?, ?, ?) "); $stmt->execute([ $target_selection_id, 'survey_urls_updated', 'Survey URLs added/updated', $_SESSION['admin_id'] ]); $pdo->commit(); header('Location: view_selection.php?id=' . $target_selection_id . '&success=1'); exit; } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } error_log("Save URLs error: " . $e->getMessage()); $error = $e->getMessage(); } } include 'includes/header.php'; ?>
Configure survey URLs for your selection(s)