Question

Greetings

I'm new with the Slim Micro Framework, and not a pro in PHP. I've been looking how similar answer i could relate with my problem and haven't had good results.

My problem is, that i'm trying to update the database trough an API, with the PUT method that this framework offers, and every time and running the request, i get

Unexpected token S

When the request header is:

Status: 200 OK Show explanation Loading time: 422 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36 Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo Content-Type: application/json Accept: */* Accept-Encoding: gzip,deflate,sdch Accept-Language: es,en-US;q=0.8,en;q=0.6 and the response header i get Date: Mon, 24 Mar 2014 20:05:14 GMT Server: Apache Access-Control-Allow-Origin: * Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 98 Keep-Alive: timeout=10, max=500 Connection: Keep-Alive Content-Type: application/json

Basically this is the code:

 $app->put('/update-user/:id', 'updateUser'); 
 function updateUser($id){

$app = \Slim\Slim::getInstance();
$app->contentType('application/json');
$app->response()->header('Access-Control-Allow-Origin', '*');
$app->response()->header('Content-Type', 'application/json');
    $response = $app->response();
$body = $app->request()->getBody();
$user = json_decode($body);

// $response = array();

$sql = "update usr SET experiencie=:experiencie WHERE id=:id";
try {
    $db = PDOConnection();
    $stmt = $db->prepare($sql) or die("Error: query preparation to database failed.");

    $stmt->bindParam("experiencie", $user->experiencie);

    $stmt->bindParam("id", $id) or die("Error:: fallo en parámetro -> id");
    $stmt->execute();
    $db = null;
    if ($stmt->execute()) {
        $response['error'] = false;
        $response['text'] = '"Good!"';
    } else {
         $response['error'] = true;
         $response['text'] = '"Bad!"';
     }

     echo json_encode($response);
} catch (PDOException $e) {
    error_log($e->getMessage(), 3, '/var/tmp/php.log');
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
 }
}

I get status 200 ok, but not any response if either good or bad, and the database does not reflects any changes.

Thanks in advance.

Was it helpful?

Solution

My guess is that the error occurs while trying to decipher your hand-rolled error JSON. Simply put, never produce JSON manually. Change your exception handler to this...

catch (PDOException $e) {
    error_log($e->getMessage(), 3, '/var/tmp/php.log');
    http_response_code(500);
    echo json_encode(['error' => ['text' => $e->getMessage()]]);
}

Assuming you have PDO set to throw exceptions, get rid of the or die() statements.

You are also calling $stmt->execute() twice as well as closing the connection before you're done with it. Your code will work better as...

try {
    if (!isset($user->experiencie)) {
        throw new Exception('Missing "experiencie" request parameter');
    }

    $db = PDOConnection();
    $stmt = $db->prepare($sql);

    $stmt->bindParam(':experiencie', $user->experiencie);

    $stmt->bindParam(':id', $id);
    if ($stmt->execute()) {
        $response['error'] = false;
        $response['text'] = 'Good!';
    } else {
         $response['error'] = true;
         $response['text'] = 'Bad!';
    }

    echo json_encode($response);
} catch (Exception $e) {
    error_log($e->getMessage(), 3, '/var/tmp/php.log');
    http_response_code(500);
    echo json_encode(['error' => ['text' => $e->getMessage()]]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top