From 105bbb2b2c982edc42c0783e98fb57b2912a9240 Mon Sep 17 00:00:00 2001 From: janis steiner Date: Thu, 7 May 2026 21:17:46 +0200 Subject: [PATCH] fixing db --- backend/api/index.php | 53 ++++++++++++++++++++++++++----------- backend/config/database.php | 2 +- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/backend/api/index.php b/backend/api/index.php index b3b4e34..177af70 100644 --- a/backend/api/index.php +++ b/backend/api/index.php @@ -15,7 +15,7 @@ require_once __DIR__ . '/../config/database.php'; $db = getDbConnection(); // Ensure notes column exists -try { $db->exec("ALTER TABLE network_nodes ADD COLUMN notes TEXT DEFAULT '' AFTER group_name"); } catch (Exception $e) {} +try { $db->exec("ALTER TABLE network_nodes ADD COLUMN notes TEXT DEFAULT ''"); } catch (Exception $e) {} // Auth check (except for session check) $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); @@ -370,20 +370,37 @@ function handleNodes($method, $id, $db) { break; case 'POST': $data = json_decode(file_get_contents('php://input'), true); - $stmt = $db->prepare(" - INSERT INTO network_nodes (label, ip_address, node_type, status, group_name, pos_x, pos_y, notes) - VALUES (?, ?, ?, ?, ?, ?, ?, ?) - "); - $stmt->execute([ - $data['label'], - $data['ip_address'] ?? '', - $data['node_type'] ?? 'host', - $data['status'] ?? 'unknown', - $data['group_name'] ?? 'default', - $data['pos_x'] ?? 0, - $data['pos_y'] ?? 0, - $data['notes'] ?? '' - ]); + try { + $stmt = $db->prepare(" + INSERT INTO network_nodes (label, ip_address, node_type, status, group_name, pos_x, pos_y, notes) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) + "); + $stmt->execute([ + $data['label'], + $data['ip_address'] ?? '', + $data['node_type'] ?? 'host', + $data['status'] ?? 'unknown', + $data['group_name'] ?? 'default', + $data['pos_x'] ?? 0, + $data['pos_y'] ?? 0, + $data['notes'] ?? '' + ]); + } catch (Exception $e) { + // Fallback without notes column + $stmt = $db->prepare(" + INSERT INTO network_nodes (label, ip_address, node_type, status, group_name, pos_x, pos_y) + VALUES (?, ?, ?, ?, ?, ?, ?) + "); + $stmt->execute([ + $data['label'], + $data['ip_address'] ?? '', + $data['node_type'] ?? 'host', + $data['status'] ?? 'unknown', + $data['group_name'] ?? 'default', + $data['pos_x'] ?? 0, + $data['pos_y'] ?? 0 + ]); + } echo json_encode(['id' => $db->lastInsertId()]); break; case 'PUT': @@ -391,7 +408,7 @@ function handleNodes($method, $id, $db) { $data = json_decode(file_get_contents('php://input'), true); $fields = []; $params = []; - foreach (['label','ip_address','node_type','status','group_name','pos_x','pos_y','notes'] as $f) { + foreach (['label','ip_address','node_type','status','group_name','pos_x','pos_y'] as $f) { if (isset($data[$f])) { $fields[] = "$f = ?"; $params[] = $data[$f]; @@ -402,6 +419,10 @@ function handleNodes($method, $id, $db) { $stmt = $db->prepare("UPDATE network_nodes SET " . implode(', ', $fields) . " WHERE id = ?"); $stmt->execute($params); } + // Update notes separately (column may not exist) + if (isset($data['notes'])) { + try { $db->prepare("UPDATE network_nodes SET notes = ? WHERE id = ?")->execute([$data['notes'], $id]); } catch (Exception $e) {} + } echo json_encode(['updated' => true]); } break; diff --git a/backend/config/database.php b/backend/config/database.php index 9763434..0e644a8 100644 --- a/backend/config/database.php +++ b/backend/config/database.php @@ -39,7 +39,7 @@ function migrate($db) { created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); try { - $db->exec("ALTER TABLE network_nodes ADD COLUMN notes TEXT DEFAULT '' AFTER group_name"); + $db->exec("ALTER TABLE network_nodes ADD COLUMN notes TEXT DEFAULT ''"); } catch (Exception $e) { } } \ No newline at end of file