0


I'm trying to build a simple REST application with Slim framework, but I'm getting a 500 error when I try to execute the POST request. So far, I've implemented two working GET requests. Here is the code:

index.php:

require_once '../include/DbHandler.php';
require '.././libs/Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

$app->get("/", function () {
    echo "<h1>Hello!!!!</h1>";
});

/**
 * Get all the events
 * method GET
 * url /events
 */
$app->get('/events', function() {
    $db = new DbHandler();
    $response = array();

    // fetch events
    $result = $db->getAllEvents();

    if ($result != NULL) {
        $response["error"] = false;
        $response = $result;
        echoResponse(200, $response);
    } else {
        $response["error"] = true;
        $response["message"] = "The requested resource doesn't exists";
        echoResponse(404, $response);
    }
});

$app->get('/event/:id', function ($id) {
    $response = array();
    $db = new DbHandler();

    // fetch event
    $result = $db->getEvent($id);

    if ($result != NULL) {
        $response["error"] = false;
        $response = $result;
        echoResponse(200, $response);
    } else {
        $response["error"] = true;
        $response["message"] = "The requested resource doesn't exists";
        echoResponse(404, $response);
    }
});

$app->post('/events', function() {
    // opening db connection
    $db = new DbConnect();

    //$db = new DbHandler();

    $request = Slim::getInstance()->request();

    //$result = $db->addEvent($request);

    $event = json_decode($request->getBody());

    $img = " ";
    $sql = "INSERT INTO event (title, location, date_event, ageMin, ageMax, groupSize, limited, maxParticipants, joining, description, img, type, language) VALUES (".$event->title.", ".$event->location.", ".$event->date_event.", ".$event->ageMin.", ".$event->ageMax.", ".$event->ageMax.", ".$event->limited.", ".$event->maxParticipants.", ".$event->joining.", ".$event->description.", ".$img.", "$event->type", ".$event->language.")";
    try {
        //$db = $this->conn;
        $conn = $db->connect();
        $result = $conn->query($sql);
        $event->id = $conn->lastInsertId();
        echoResponse(200, $event->id);
    } catch(PDOException $e) {
        echoResponse(404, '{"error":{"text":'. $e->getMessage() .'}}');
    }
});

/**
 * Echoing json response to client
 * @param String $status_code Http response code
 * @param Int $response Json response
 */
function echoResponse($status_code, $response) {
    $app = \Slim\Slim::getInstance();
    // Http response code
    $app->status($status_code);

    // setting response content type to json
    $app->contentType('application/json');

    echo json_encode($response);
}

$app->run();

?>

As I said before, the two GET methods are working, but when I try to add a row with the POST method using this data:

{"title": "Test 1", "location": “Rome, Italy", "date_event": "2016-05-12", "time": "22:00:00", "ageMin": 21, "ageMax": 27, "groupSize": "3", "limited": false, "maxParticipants": "", "joining": "2", "description": "Description test 1", "img": "", "type": "hanging out", "language": "Italian/English"}

I get 500 Internal Server Error and I cannot understand why.
Where am I doing wrong?
Thanks!

DamianFox
  • 826
  • 3
  • 17
  • 41

1 Answers1

2

You've missed concatenation dots around "$event->type" in your $sql assignment.


While developing I recommend you to enable error and warning messages adding

error_reporting(E_ALL);
ini_set('display_errors', 1);

to your code.

PHP Syntax Check: Parse error: syntax error, unexpected '$event' (T_VARIABLE) in your code on line 62.

is more likely understandable than

500 Internal Server Error.

As an alternative, if you have a small piece of code to check, you can use some online tool like http://phpcodechecker.com/

Jordi Nebot
  • 3,042
  • 3
  • 25
  • 40
  • How did you get this message `PHP Syntax Check: Parse error: syntax error, unexpected '$event' (T_VARIABLE) in your code on line 62.`? Did you use a command? – DamianFox May 12 '16 at 14:49
  • I've just used [phpcodechecker.com](http://phpcodechecker.com/). But as I've said, I recommend you to enable error reporting in your development environment. There are other SO questions [on this topic](http://stackoverflow.com/q/1053424/1534704). – Jordi Nebot May 12 '16 at 14:55