php - 来自 php 服务器的 Json 数据不起作用。

标签 php android mysql json

我正在MYSQL数据库中上传数据,同时我想检索我已插入的属性之一,以满足我的成功上传。当我第一次按下按钮时,它只将数据上传到服务器,而不返回任何内容。同样,当我点击按钮时,它会同时执行两个过程(插入和检索数据),因此我无法第一次以 json 对象的形式返回值。

这是我的 php 代码 engrdatainsert.php

 <?php  
$sqlCon=mysql_connect("localhost","root","");
mysql_select_db("PeopleData");
 //Retrieve the data from the Android Post done by and Engr...
 $adp_no = $_REQUEST['adp_no'];
 $building_no = $_POST['building_no'];
 $contractor_name = $_POST['contractor_name'];
 $officer_name = $_POST['officer_name'];
  $area = $_POST['area'];

--------------------插入从Android接收到的值----------||

   $sql = "INSERT INTO engrdata (adp_no, building_no,area,contractor_name,officer_name)     VALUES('$adp_no', '$building_no', '$are',  '$contractor_name', '$officer_name')";

//--------现在查看插入数据的事务状态---------||

 $q=mysql_query("SELECT adp_no FROM engrdata WHERE adp_no='$adp_no'");
   while($e=mysql_fetch_assoc($q))
    $output[]=$e;
print(json_encode($output));//conveting into json array
   mysql_close();
     ?>

我的 Android 代码

 public void insertdata()
   {
 InputStream is=null;
 String result=null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
        nameValuePairs.add(new BasicNameValuePair("adp_no",adp));//"34")); 
    nameValuePairs.add(new BasicNameValuePair("building_no",bldng));//"72"));
    nameValuePairs.add(new BasicNameValuePair("area",myarea));//"72"));
    nameValuePairs.add(new BasicNameValuePair("contractor_name",cntrct));//"72"));
    nameValuePairs.add(new BasicNameValuePair("officer_name",ofcr));//"72"));
    //http post
       try{
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new   HttpPost("http://10.0.2.2/androidconnection/engrdatainsert.php"); 
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.i("postData", response.getStatusLine().toString());
    }

    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection "+e.toString());
    }   
 //convert the input strem into a string value
    try
      {
            BufferedReader reader = new BufferedReader(new    InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) 
            {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }
    catch(Exception e)
    {   Log.e("log_tag", "Error converting result "+e.toString());   }
    try
    {
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++)
        {
            JSONObject json_data = jArray.getJSONObject(i);
           Toast.makeText(this, "data is "+json_data.getString("adp_no")+"\n",   Toast.LENGTH_LONG).show();
           String return_val = json_data.getString("adp_no");
           if(return_val!=null)
           {
             Intent offff=new Intent(this,MainActivity.class);
              offff.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
              offff.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              //startActivity(offff);
           }
        }
    }
    //}
    catch(JSONException e)
    {      Log.e("log_tag", "Error parsing data "+e.toString());        }
   // return returnString;//*/
    }

最佳答案

在您的 PHP 代码中,您没有执行 INSERT 查询。你需要做这样的事情:

--------------------插入从Android接收到的值----------||

$sql = "INSERT INTO engrdata (adp_no, building_no,area,contractor_name,officer_name)     VALUES('$adp_no', '$building_no', '$are',  '$contractor_name', '$officer_name')";
mysql_query($sql) or die(mysql_error());

//--------现在查看插入数据的事务状态---------||

注意我添加的行,它实际执行查询。

现在您当然应该将代码升级到 mysqlimysqlPDO因为不再支持 PHP mysql 包。

关于php - 来自 php 服务器的 Json 数据不起作用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13973355/

相关文章:

php - 如何在 Laravel 中的 Blade 模板中链接到静态资源?

android - 离线拼写检查 API

android - 如何在ionic中使用ng-repeat显示数组值

android - 我如何下载视频并将其转换为使用解析在 videoview 中播放?

mysql - 你的sql语法有误

MySQL SELECT DISTINCT 多列

javascript - 替换 jQuery 单击事件上的标题文本

php - 处理@抑制的错误的正确方法?

php - 如何通过php的header函数移动到特定的navtab

MySQL 和 ADO - 无望了吗?