Undefined variable in php while insert to mysql [duplicate]

I am getting this error that the variable is undefined when I actually set it to the value and no matter what kind of value I set even simple $user = 1 ; doesn’t work and I got error it is undefined

<?php 

session_start();

include ('get-user-data.php');

$user = array();

if(isset($_SESSION['userID'])){
    require ('connection.php');
    $userId = $_SESSION['userID'];


    function wantToRead() {
        $query = " INSERT INTO bookshelf (id, tittle, category, thumbnail) VALUES ('$userId', ' Book', 'read')";
    }
}
?>