['Required']]); } $data = []; foreach ($allowed as $col) { if (array_key_exists($col, $in)) { $data[$col] = $in[$col] !== '' ? $in[$col] : null; } } $ownerId = (int) ($in['owner_id'] ?? 0); if ($ownerId > 0) { if (!user_owns_owner($pdo, $user, $ownerId)) { json_error(403, 'No access to this owner'); } if ($data) { $set = implode(', ', array_map(fn ($c) => "$c = ?", array_keys($data))); $pdo->prepare("UPDATE property_owners SET $set WHERE owner_id = ? AND user_id = ?") ->execute([...array_values($data), $ownerId, $user['id']]); } } else { $data['user_id'] = $user['id']; // NOT NULL without a default in the legacy schema. $data['owner_type'] ??= 'individual'; $data['email'] ??= ''; $cols = implode(', ', array_keys($data)); $ph = implode(', ', array_fill(0, count($data), '?')); $pdo->prepare("INSERT INTO property_owners ($cols) VALUES ($ph)") ->execute(array_values($data)); $ownerId = (int) $pdo->lastInsertId(); } $stmt = $pdo->prepare('SELECT * FROM property_owners WHERE owner_id = ? LIMIT 1'); $stmt->execute([$ownerId]); json_data(present_owner($stmt->fetch()));