Q:

Slim framework INSERT query not working

I'm using the Slim framework to build a RESTful API for a server database. I have been racking my mind trying to figure out why my INSERT statement below is not executing. Every time I run it, I get a response saying "error: true" and the "Failed to save sound. Please try again." message. Verified this error by the fact that it's not showing up in my DB. Other INSERT queries in my API are working fine, and when I tried running this insert query on its own in MySQL it worked, but I can't see why this function wouldn't work!
in my DBHandler file:
    public function newSound($user_id, $sound, $sound_viz, $latitude, $longitude, $location, $title) {
    $response = array();

    // Insert query
    $stmt = $this->conn->prepare("INSERT INTO sounds (user_id, sound, sound_viz, latitude, longitude, location, title) VALUES (?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssddss", $user_id, $sound, $sound_viz, $latitude, $longitude, $location, $title);
    $result = $stmt->execute();
    $stmt->close();

    // Check for successful insertion
        if ($result) {
            // Sound successfully inserted
            return SOUND_CREATED_SUCCESSFULLY;
        } else {
            // Failed to post sound
            return SOUND_CREATE_FAILED;
        }

}

In my index file:
$app->post('/sounds/', function() use ($app) {
        // check for required params
        verifyRequiredParams(array('sound', 'sound_viz', 'latitude', 'longitude', 'location','title'));

        $response = array();
        $sound = $app->request->post('sound');
        $sound_viz = $app->request->post('sound_viz');
        $latitude = $app->request->post('latitude');
        $longitude = $app->request->post('longitude');
        $location = $app->request->post('location');
        $title = $app->request->post('title');

        global $user_id;
        $db = new DbHandler();

        // creating new sound
        $sound_result = $db->newSound($user_id, $sound, $sound_viz, $latitude, $longitude, $location, $title);

        if ($sound_result = SOUND_CREATED_SUCCESSFULLY) {
            $response["error"] = false;
            $response["message"] = "Sound saved successfully.";
            $response['sound_id'] = $sound_id;
            echoRespnse(201, $response);
        } else {
            $response["error"] = true;
            $response["message"] = "Failed to save sound. Please try again.";
            echoRespnse(200, $response);
        }            
    });

My DB credentials are correct, as are the variables SOUND_CREATED_SUCCESSFULLY etc. Any suggestions or advice? Thanks!

A:

After much careful debugging, it turns out that the issue is that the user_id is not being passed in properly (it's always null) because of the global variable $user_id being reset with every new HTTP request. I will have to find a way to incorporate session variables or similar to get this to work. Hope this is helpful for anyone having this issue in the future.

