diff --git a/src/edit_backpack.php b/src/edit_backpack.php index 249eeff..b7be59d 100644 --- a/src/edit_backpack.php +++ b/src/edit_backpack.php @@ -30,6 +30,11 @@ $check_col = $conn->query("SHOW COLUMNS FROM backpacks LIKE 'product_url'"); if ($check_col && $check_col->num_rows == 0) { $conn->query("ALTER TABLE backpacks ADD COLUMN product_url VARCHAR(255) DEFAULT NULL AFTER image_url"); } +$check_col_cat = $conn->query("SHOW COLUMNS FROM backpacks LIKE 'category_id'"); +if ($check_col_cat && $check_col_cat->num_rows == 0) { + $conn->query("ALTER TABLE backpacks ADD COLUMN category_id INT DEFAULT NULL"); + $conn->query("ALTER TABLE backpacks ADD CONSTRAINT fk_bp_category FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL"); +} $check_col_c = $conn->query("SHOW COLUMNS FROM backpack_compartments LIKE 'linked_article_id'"); if ($check_col_c && $check_col_c->num_rows == 0) { $conn->query("ALTER TABLE backpack_compartments ADD COLUMN linked_article_id INT DEFAULT NULL"); @@ -103,14 +108,7 @@ if ($backpack_id > 0) { } } -// Load Manufacturers -$stmt_man_load = $conn->prepare("SELECT id, name FROM manufacturers WHERE user_id = ? ORDER BY name ASC"); -$stmt_man_load->bind_param("i", $user_id); -$stmt_man_load->execute(); -$manufacturers = $stmt_man_load->get_result()->fetch_all(MYSQLI_ASSOC); -$stmt_man_load->close(); - -// Load Articles for Linked Compartments +// Load Manufacturers & Categories $hh_ids = [$user_id]; if ($household_id) { $stmt_hhm = $conn->prepare("SELECT id FROM users WHERE household_id = ?"); @@ -121,6 +119,22 @@ if ($household_id) { } $placeholders = implode(',', array_fill(0, count($hh_ids), '?')); $types_hh = str_repeat('i', count($hh_ids)); + +// Manufacturers +$stmt_man_load = $conn->prepare("SELECT id, name FROM manufacturers WHERE user_id IN ($placeholders) ORDER BY name ASC"); +$stmt_man_load->bind_param($types_hh, ...$hh_ids); +$stmt_man_load->execute(); +$manufacturers = $stmt_man_load->get_result()->fetch_all(MYSQLI_ASSOC); +$stmt_man_load->close(); + +// Categories +$stmt_cat_load = $conn->prepare("SELECT id, name FROM categories WHERE user_id IN ($placeholders) ORDER BY name ASC"); +$stmt_cat_load->bind_param($types_hh, ...$hh_ids); +$stmt_cat_load->execute(); +$categories = $stmt_cat_load->get_result()->fetch_all(MYSQLI_ASSOC); +$stmt_cat_load->close(); + +// Load Articles for Linked Compartments $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]); $types_arts = $types_hh . 'i'; @@ -134,6 +148,7 @@ $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') { @@ -169,6 +184,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $volume = intval($_POST['volume_liters']); $share_household = isset($_POST['share_household']) ? 1 : 0; $product_url_input = trim($_POST['product_url'] ?? ''); + $category_id = !empty($_POST['category_id']) ? intval($_POST['category_id']) : NULL; $image_url_for_db = $image_url; $pasted_image = $_POST['pasted_image_data'] ?? ''; @@ -194,12 +210,12 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $final_household_id = ($share_household && $household_id) ? $household_id : NULL; if ($backpack_id > 0) { - $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 = $conn->prepare("UPDATE backpacks SET name=?, manufacturer=?, model=?, weight_grams=?, volume_liters=?, household_id=?, image_url=?, product_url=?, category_id=? WHERE id=? AND user_id=?"); + $stmt->bind_param("sssiisssiii", $name, $manufacturer, $model, $weight, $volume, $final_household_id, $image_url_for_db, $product_url_input, $category_id, $backpack_id, $user_id); $stmt->execute(); } else { - $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 = $conn->prepare("INSERT INTO backpacks (user_id, household_id, name, manufacturer, model, weight_grams, volume_liters, image_url, product_url, category_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $stmt->bind_param("iisssiissi", $user_id, $final_household_id, $name, $manufacturer, $model, $weight, $volume, $image_url_for_db, $product_url_input, $category_id); $stmt->execute(); $backpack_id = $stmt->insert_id; } @@ -309,6 +325,19 @@ require_once 'header.php'; +