2017-12-09 2 views
0

本質的に、私はトラブルシューティングのヘルプを探しています。これは先週働いて、私は何も変えなかった。

ローカルマシンでApacheを実行していて、ChromeプラグインのREST Consoleを使用して自分のURLにPOSTを送信しています。私が言及したように、先週はうまくいきました。今週、JSONオブジェクトにnull値または空の値があると言うエラーが表示されます。エラーメッセージは次のとおりです。

{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 
'id' cannot be null}id was: 

マイコード:

<?php 
use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response; 

require 'vendor/autoload.php'; 
function connect_to_db() { 
    $conn = new PDO('mysql:host=localhost;dbname=moviedb','movieuser','ONVYyc5FiBPcKaK4'); 
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 
    return $conn; 
} 

$app = new \Slim\App; 


$app->post('/movies/add', function (Request $request, Response $response) { 
    $id = $request->getParam('id'); 
    $name = $request->getParam('name'); 
    $description = $request->getParam('description'); 
    $stars = $request->getParam('stars'); 
    $length = $request->getParam('length'); 
    $image = $request->getParam('image'); 
    $year = $request->getParam('year'); 
    $rating = $request->getParam('rating'); 
    $director = $request->getParam('director'); 
    $url = $request->getParam('url'); 
    $sql_query = "INSERT INTO movies (id,name,description,stars,length,image,year,rating,director,url) VALUES (:id,:name,:description,:stars,:length,:image,:year,:rating,:director,:url)"; 
    try{ 
     $db = connect_to_db(); 
     $stmt = $db->prepare($sql_query); 
     $stmt->bindParam(':id',$id); 
     $stmt->bindParam(':name',$name); 
     $stmt->bindParam(':description',$description); 
     $stmt->bindParam(':stars',$stars); 
     $stmt->bindParam(':length',$length); 
     $stmt->bindParam(':image',$image); 
     $stmt->bindParam(':year',$year); 
     $stmt->bindParam(':rating',$rating); 
     $stmt->bindParam(':director',$director); 
     $stmt->bindParam(':url',$url); 
     $stmt->execute(); 
     $db = null; 
     echo '{"Result":{"text":"Added"}'; 
    } 
    catch (PDOException $e) { 
     echo '{"error":{"text":'.$e->getMessage().'}'.'id was: '.$id; 
    } 
}); 

$app->run(); 

とJSON私がテストに使用:

{"id":"test", 
"name":"test movie", 
"description":"test movie", 
"stars":"test movie", 
"length":"test movie", 
"image":"test movie", 
"year":"test movie", 
"rating":"test movie", 
"director":"test movie", 
"url":"test movie" 
} 

私はこれをトラブルシューティングするために何ができますか? POSTと私のapacheサーバーとの間で、そしてJSONを取り除くかもしれないphpの間に何が起こるのですか? JSONが$ request変数にある場合、どうすればそれを見ることができますか?これが動作を停止させる原因となった構成は何ですか?

答えて

0

content-typeapplication/jsonを手動で設定すると、AndroidアプリとRESTコンソールの両方でその操作が完了しました。なぜ以前にそのパラメータを設定せずに働いたのかまだ分かりません。

関連する問題