带有POST请求的500个内部服务器错误-Slim框架 [英] 500 internal server error with POST request - Slim framework

查看:100
本文介绍了带有POST请求的500个内部服务器错误-Slim框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用Slim框架构建一个简单的REST应用程序,但是当我尝试执行POST请求时遇到500错误.到目前为止,我已经实现了两个有效的GET请求.这是代码:


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 :

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();

?>

正如我之前所说,这两种GET方法都可以使用,但是当我尝试使用POST方法使用此数据添加一行时:

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"}

我得到500 Internal Server Error,但我不明白为什么.
我在哪里做错了?
谢谢!

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

推荐答案

$sql分配中,您错过了"$event->type"周围的串联点.

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);

输入您的代码.

PHP语法检查:解析错误:语法错误,您的代码在第62行出现意外的'$ event'(T_VARIABLE).

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

500内部服务器错误.

500 Internal Server Error.

或者,如果您需要检查一小段代码,则可以使用一些在线工具,例如 http://phpcodechecker .com/

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

这篇关于带有POST请求的500个内部服务器错误-Slim框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆