0

我是 php 新手。我将数据从 android 发布到 php 服务器 as--

JSONObject jsonObject = new JSONObject();
try
{
    jsonObject.put("name", "john");
    String url = "http://10.0.2.2/WebService/submitname.php";
    HttpClient client = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("json", jsonObject.toString());
    StringEntity se = null;
    se = new StringEntity(jsonObject.toString());
    se.setContentEncoding(new BasicHeader(
    HTTP.CONTENT_TYPE, "application/json"));
    httpPost.setEntity(se);
    HttpResponse response = client.execute(httpPost);
    int i = response.getStatusLine().getStatusCode();
    Log.v("status", "" + i);
} catch (Exception e)
{
    e.printStackTrace();
} 

并在 php 中接收数据为

<?php
 mysql_connect("localhost","root","");
 mysql_select_db("my db");
 $var = json_decode($_POST['HTTP_JSON']);
 $service = $var->{'name'};
 mysql_query("INSERT INTO name_table(`_id`, `retrived_name`, `cat`, `is_valid_name`) VALUES (1547, '$service','$var',true);");
 echo  $var;
?>

在服务器端一无所获。但是 php 查询执行正确,因为得到 200 响应和数据库中的新行,但字段为 retrived_namecat

我该如何解决这个问题?提前致谢!

4

1 回答 1

0

您应该在查询中提供值:

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("my db");
    $var = json_decode($_POST['HTTP_JSON']);
    $service = $var->{'name'};
    $name = $_POST['name'];
    mysql_query("INSERT INTO name_table(`_id`, `retrived_name`, `cat`, `is_valid_name`) VALUES (1547, '$service','$name',true);");
    echo  $var;
?>
于 2016-06-07T09:37:58.683 回答