submit_activities.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ?>
  41. <!DOCTYPE html>
  42. <html lang="en">
  43. <head>
  44. <meta charset="UTF-8">
  45. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  46. <title>Your packing list</title>
  47. <style>
  48. body {
  49. font-family: Arial, sans-serif;
  50. padding: 20px;
  51. }
  52. .item-item {
  53. margin: 5px 0;
  54. }
  55. .item-item input {
  56. margin-right: 10px;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <h1>You need</h1>
  62. <?php
  63. // Display results as HTML with checkboxes
  64. echo "<form method='post' action='index.php'>";
  65. echo "<ul>";
  66. foreach ($items as $item) {
  67. echo "
  68. <div class='item-item'>
  69. <input type='checkbox' name='selected_items' value='" . htmlspecialchars($item['itemID']) . "'>
  70. <label for=selected_items_" . htmlspecialchars($item['itemID']) . ">"
  71. . htmlspecialchars($item['itemName'])
  72. . "</label></div>";
  73. }
  74. echo "</ul>";
  75. echo "</form>";
  76. ?>
  77. </body>
  78. </html>