現在、サーバーとやり取りするAndroidアプリを開発中です(現在はローカル)。 これはPHPを使用してJavaコードをサーバーと通信しています。 これは私がPHPを初めて使用するため、これで苦労しています。POST変数の値を設定できないのはなぜですか?
何らかの理由で、データベースに保存する必要があるすべての値がnullです。PHPコードをテストするときに、アプリケーションから取得するかURLから取得するかは関係ありません。私が得る唯一のメッセージは次のとおりです。
{"成功":0、 "メッセージ": "必須フィールドはありません"}
私はprint_r($ _POST)を試しましたが、私は得た:Array()、それは何を意味するのか分かりません。私はインターネット上で見たこともすべて成功した。ここで
は、PHPのコードです:
<?php
$response = array();
if (!empty($_POST['name']) && !empty($_POST['breed']) && !empty($_POST['type']) && !empty($_POST['description']) && !empty($_POST['images']) && !empty($_POST['coords'])) {
$name = $_POST['name'];
$breed = $_POST['breed'];
$type = $_POST['type'];
$description = $_POST['description'];
$images = $_POST['images'];
$coords = $_POST['coords'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = "INSERT INTO lost_pets (name, breed, type, description, images, coords) VALUES(':name', ':breed', ':type', ':description', ':images', ':coords')";
$stmt = $db->prepare($result);
$stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
$stmt->bindParam(':breed', $_POST['breed'], PDO::PARAM_STR);
$stmt->bindParam(':type', $_POST['type'], PDO::PARAM_STR);
$stmt->bindParam(':description', $_POST['description'], PDO::PARAM_STR);
$stmt->bindParam(':images', $_POST['images'], PDO::PARAM_STR);
$stmt->bindParam(':coords', $_POST['coords'], PDO::PARAM_STR);
$stmt->execute();
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
私は、問題は、このコードではなく、Javaコードであるかなり確信しています。私はここで、助けてくれることを祈っています!
エラーを取得し、ここに投稿してください。 –
postmanを使って、サーバの応答が正しいかどうかを確認することができます。 –
'print_r($ _POST)を試しましたが、私は:Array()、それが何を意味するのかわかりません。 $ _POST配列が空であることを意味します。 – Spectarion