0) { // Update: own property only. if (!user_owns_home($pdo, $user, $idhome)) { json_error(403, 'No access to this home'); } if ($data) { $set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data))); $stmt = $pdo->prepare("UPDATE home SET $set WHERE idhome = ? AND iduser = ?"); $stmt->execute([...array_values($data), $idhome, $user['id']]); } } else { // Create. // These columns are NOT NULL without a default in the legacy schema. foreach (['name', 'address', 'zip', 'city', 'country', 'cadastral_municipality', 'latitude', 'longitude', 'fulladdress'] as $col) { $data[$col] ??= ''; } $data['iduser'] = $user['id']; $cols = implode(', ', array_keys($data)); $ph = implode(', ', array_fill(0, count($data), '?')); $stmt = $pdo->prepare("INSERT INTO home ($cols) VALUES ($ph)"); $stmt->execute(array_values($data)); $idhome = (int) $pdo->lastInsertId(); } $stmt = $pdo->prepare('SELECT * FROM home WHERE idhome = ? LIMIT 1'); $stmt->execute([$idhome]); json_data(present_home($stmt->fetch(), true, home_counts($pdo, [$idhome])[$idhome] ?? null));