[英]Not getting the JSON data in PHP
我正在创建一个android应用程序,并通过JSON将来自Android应用程序的用户详细信息提交到服务器端的php。 但是在服务器端,我没有得到JSON数据。 在服务器端php,来自android应用程序的JSON似乎为null。 我为此使用POST方法。 我知道我的POST方法代码在服务器端的PHP中是错误的。 但是我不知道要解决这个问题。
这是我获取JSON的PHP代码。 每当我收到相同的消息时:
必填字段缺失
码:
<?php
/*
* Following code will create a new user row
* All user details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
print_r($_POST);
// check for required fields
if (isset($_POST['Firstname']) && isset($_POST['Lastname']) && isset($_POST['Username']) && isset($_POST['Email']) && isset($_POST['Password']) && isset($_POST['Country']) && isset($_POST['Mobile'])) {
echo('bhargavi');
$Firstname = $_POST['Firstname'];
$Lastname = $_POST['Lastname'];
$Username = $_POST['Username'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
$Country = $_POST['Country'];
$Mobile = $_POST['Mobile'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO users(Firstname,Lastname,Username,Email,Password,Country,Mobile) VALUES('$Firstname','$Lastname','$Username','$Email','$Password','$Country','$Mobile')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "User successfully Registered.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
可能您应该在检查$ _POST ['Firstname']等之前解码json字符串...
假设您从应用程序发送了一个名为data的字符串。 在php上,您应该执行以下操作:
$data = json_decode($_POST['data']);
and then check if $data contains the data you neeed by doing:
if (isset($data->Firstname) && isset($data->Lastname) etc...){
...
...
}
如果我可以给您一个建议,最好是直接从您的应用程序检查数据完整性,而不是在服务器上进行检查,这将有机会节省一些服务器资源...
正如杰伊·布兰查德(Jay Blanchard)已经提到的那样,不要像这样使用mysql,否则您将遭受mysql注入的困扰...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.