diff --git a/src/edit_backpack.php b/src/edit_backpack.php index 5d17b08..249eeff 100644 --- a/src/edit_backpack.php +++ b/src/edit_backpack.php @@ -111,10 +111,8 @@ $manufacturers = $stmt_man_load->get_result()->fetch_all(MYSQLI_ASSOC); $stmt_man_load->close(); // Load Articles for Linked Compartments -// Filter: Show all articles (or maybe only containers/bags? User said "any"). Let's load all. $hh_ids = [$user_id]; if ($household_id) { - // Get all users in household $stmt_hhm = $conn->prepare("SELECT id FROM users WHERE household_id = ?"); $stmt_hhm->bind_param("i", $household_id); $stmt_hhm->execute(); @@ -124,7 +122,7 @@ if ($household_id) { $placeholders = implode(',', array_fill(0, count($hh_ids), '?')); $types_hh = str_repeat('i', count($hh_ids)); $stmt_arts = $conn->prepare("SELECT id, name, weight_grams, image_url FROM articles WHERE user_id IN ($placeholders) OR household_id = ? ORDER BY name ASC"); -$params_arts = array_merge($hh_ids, [$household_id ?: 0]); // 0 if null +$params_arts = array_merge($hh_ids, [$household_id ?: 0]); $types_arts = $types_hh . 'i'; $stmt_arts->bind_param($types_arts, ...$params_arts); $stmt_arts->execute(); @@ -136,7 +134,6 @@ $stmt_arts->close(); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $name = trim($_POST['name']); - // Manufacturer Logic $manufacturer = ''; if (isset($_POST['manufacturer_select'])) { if ($_POST['manufacturer_select'] === 'new') { @@ -173,7 +170,6 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $share_household = isset($_POST['share_household']) ? 1 : 0; $product_url_input = trim($_POST['product_url'] ?? ''); - // Image Handling $image_url_for_db = $image_url; $pasted_image = $_POST['pasted_image_data'] ?? ''; $url_image = trim($_POST['image_url_input'] ?? ''); @@ -198,12 +194,10 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $final_household_id = ($share_household && $household_id) ? $household_id : NULL; if ($backpack_id > 0) { - // Update $stmt = $conn->prepare("UPDATE backpacks SET name=?, manufacturer=?, model=?, weight_grams=?, volume_liters=?, household_id=?, image_url=?, product_url=? WHERE id=? AND user_id=?"); $stmt->bind_param("sssiisssii", $name, $manufacturer, $model, $weight, $volume, $final_household_id, $image_url_for_db, $product_url_input, $backpack_id, $user_id); $stmt->execute(); } else { - // Insert $stmt = $conn->prepare("INSERT INTO backpacks (user_id, household_id, name, manufacturer, model, weight_grams, volume_liters, image_url, product_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param("iisssiiss", $user_id, $final_household_id, $name, $manufacturer, $model, $weight, $volume, $image_url_for_db, $product_url_input); $stmt->execute(); @@ -211,14 +205,12 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } // Handle Compartments - // Arrays: comp_ids, comp_types (text/article), comp_names, comp_articles if (isset($_POST['comp_types'])) { $types = $_POST['comp_types']; $ids = $_POST['comp_ids'] ?? []; $names = $_POST['comp_names'] ?? []; $articles = $_POST['comp_articles'] ?? []; - // Get existing IDs $existing_ids = []; if($backpack_id > 0){ $stmt_check = $conn->prepare("SELECT id FROM backpack_compartments WHERE backpack_id = ?"); @@ -238,8 +230,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($c_type === 'article') { $c_article_id = intval($articles[$i] ?? 0); - if ($c_article_id <= 0) continue; // Skip invalid - // Get name from article for display purposes (fallback) + if ($c_article_id <= 0) continue; foreach($all_articles as $art) { if ($art['id'] == $c_article_id) { $c_name = $art['name']; @@ -252,20 +243,17 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { } if ($c_id > 0 && in_array($c_id, $existing_ids)) { - // Update $stmt_up = $conn->prepare("UPDATE backpack_compartments SET name = ?, sort_order = ?, linked_article_id = ? WHERE id = ?"); $stmt_up->bind_param("siii", $c_name, $i, $c_article_id, $c_id); $stmt_up->execute(); $kept_ids[] = $c_id; } else { - // Insert $stmt_in = $conn->prepare("INSERT INTO backpack_compartments (backpack_id, name, sort_order, linked_article_id) VALUES (?, ?, ?, ?)"); $stmt_in->bind_param("isii", $backpack_id, $c_name, $i, $c_article_id); $stmt_in->execute(); } } - // Delete removed foreach ($existing_ids as $ex_id) { if (!in_array($ex_id, $kept_ids)) { $conn->query("DELETE FROM backpack_compartments WHERE id = $ex_id"); @@ -380,15 +368,17 @@ require_once 'header.php'; - + - - + +
@@ -416,7 +406,7 @@ require_once 'header.php'; - + @@ -475,7 +465,6 @@ document.addEventListener('DOMContentLoaded', function() { }); // Template for new row (with placeholders) - // Note: We use a class 'tom-select-init' to mark selects that need init const rowTemplate = `