【问题标题】:Retrieve wrong data检索错误数据
【发布时间】:2016-05-24 12:37:37
【问题描述】:

这是 MySQL 数据(表工作详情)

通过以下代码检索到的数据。注意到我想检索除 idtwd 之外的所有内容。

 private void showForce(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray result = jsonObject.getJSONArray(Configs.TAG_JSON_ARRAY);
            JSONObject c = result.getJSONObject(0);
            String project = c.getString(Configs.TAG_PROJECT).trim();
            RetrieveProject(project);
            String description = c.getString(Configs.TAG_WORKDESCRIPTION).trim();
            int per = c.getInt(Configs.TAG_PERCENTAGE);
            String in=c.getString(Configs.TAG_IN).trim();
            String time[]=in.split(":", 3);
            String time1=time[0]+":"+time[1];
            String out=c.getString(Configs.TAG_OUT).trim();
            String timeOut[]=out.split(":", 3);
            String timeout=timeOut[0]+":"+timeOut[1];
            seekBar.setProgress(per);
            progressText.setText("Covered:" + "" + seekBar.getProgress() + "/" + seekBar.getMax());
            String From=c.getString(Configs.TAG_From).trim();
            String To=c.getString(Configs.TAG_To).trim();
            String Mil=c.getString(Configs.TAG_Mileage).trim();
            String Hotel=c.getString(Configs.TAG_Hotel).trim();
            String Toll=c.getString(Configs.TAG_Toll).trim();
            String Business=c.getString(Configs.TAG_Business).trim();

            TimeIn.setText(time1);
            TimeOut.setText(timeout);
            Description.setText(description);
            mileage.setText(Mil);
            hotel.setText(Hotel);
            business.setText(Business);
            toll.setText(Toll);
            travelFrom.setText(From);
            travelTo.setText(To);

        } catch (JSONException e) {
            e.printStackTrace();
        }

PhP

<?php
 define('HOST','localhost');
    define('USER','spiral69_wp178');
    define('PASS','q1w2e3r4');
    define('DB','spiral69_androiddb');

  $con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');

  $id=$_GET['id'];

 $sql = "select * from work_details WHERE id= '". $id."'";

  $res = mysqli_query($con,$sql);

  $result=array();

 while($row=mysqli_fetch_array($res)){
      array_push($result,array('id'=>$row[0],'project'=>$row[1],'work_description'=>$row[2],'percentage'=>$row[3],'timeIn'=>$row[4],
      'timeOut'=>$row[5],'travel_From'=>$row[6],'travel_To'=>$row[7],'mileage'=>$row[8],'hotel_accom'=>$row[9],'toll'=>$row[10],'business_expenses'=>$row[11]));
  }

 echo (json_encode(array("result"=>$result)));

mysqli_close($con);

?>

Project,Description,TimeIn,TimeOut,per 显示正确,但 From, To, Mil,Hotel,Toll,Business 不正确。

这是我在setText 中得到的输出。(不正确)

 travelFrom.setText(From); I get 63 (twd column)
   travelTo.setText(To); I get df (travel_from column)
   mileage.setText(Mil); I get a....

【问题讨论】:

  • 您的代码易受 SQL 注入攻击!

标签: java php android mysql json


【解决方案1】:

只是您在 PHP 脚本中的 json 对象中设置数据时错过了“twd”列...“travel_from”的索引是 7 而不是 6,之后的其他字段同样适用!! !

【讨论】:

  • 但我不想在 twd 列中检索数据
  • 'travel_From'=>$row[7],'travel_To'=>$row[8],'mileage'=>$row[8] .......... .......等等
  • 您需要更改数据重试查询然后.... select * from work_details WHERE id= '".$id."'" ...... ..........您需要更新它以仅获取您想要的列
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多