submit_activities.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // Database configuration
  3. require_once '../config.php';
  4. try {
  5. // Establish a database connection using PDO
  6. $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password, [
  7. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  8. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  9. ]);
  10. } catch (PDOException $e) {
  11. die("Database connection failed: " . $e->getMessage());
  12. }
  13. // Retrieve and sanitize GET parameters
  14. $activityIds = isset($_GET['activity_ids']) ? $_GET['activity_ids'] : '';
  15. $activityIds = explode(',', $activityIds);
  16. $sanitizedIds = [];
  17. foreach ($activityIds as $id) {
  18. if (ctype_digit($id) && (int)$id >= 0 && (int)$id <= 256) {
  19. $sanitizedIds[] = (int)$id;
  20. }
  21. }
  22. if (empty($sanitizedIds)) {
  23. die("No valid activity IDs provided.");
  24. }
  25. // Create a query to get itemIDs
  26. $placeholders = implode(',', array_fill(0, count($sanitizedIds), '?'));
  27. $sql = "SELECT itemID FROM item_activity_map WHERE activityID IN ($placeholders)";
  28. $stmt = $pdo->prepare($sql);
  29. $stmt->execute($sanitizedIds);
  30. $itemIDs = $stmt->fetchAll(PDO::FETCH_COLUMN);
  31. if (empty($itemIDs)) {
  32. die("No matching item IDs found.");
  33. }
  34. // Retrieve item names based on itemIDs
  35. $placeholders = implode(',', array_fill(0, count($itemIDs), '?'));
  36. $sql = "SELECT itemID, itemName FROM item_names WHERE itemID IN ($placeholders)";
  37. $stmt = $pdo->prepare($sql);
  38. $stmt->execute($itemIDs);
  39. $items = $stmt->fetchAll();
  40. // Display results as HTML with checkboxes
  41. echo "<form method='post' action='process_selection.php'>";
  42. echo "<ul>";
  43. foreach ($items as $item) {
  44. echo "<li><input type='checkbox' name='selected_items[]' value='" . htmlspecialchars($item['itemID']) . "'> " . htmlspecialchars($item['itemName']) . "</li>";
  45. }
  46. echo "</ul>";
  47. echo "<button type='submit'>Submit</button>";
  48. echo "</form>";
  49. ?>