来自nameValuePairs的值未成功传递到数据库

时间:2019-02-25 12:38:14

标签: php android

我正在使用nameValuePairs将数据传递到数据库。其中的值来自地图。我将其放入字符串中,然后将字符串放入nameValuePairs中。我的PHP似乎工作正常。我的目标是使用SQL语句中的where更新包含计数的表。

<?php

$conn = mysqli_connect("localhost", "root", "", "nearbyme");
$namelocation = $_GET['namelocation'];
$count = $_GET['count'];

$sql = "UPDATE place SET count= '$count' WHERE namelocation='$namelocation' ";
$result = mysqli_query($conn, $sql);

if($result){
echo "Updated!";
}else{
echo "failed"; 
}
?>

这是我使用nameValuePairs传递数据的活动。

MapsActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
RequestQueue requestQueue;
private List<LocationModel> mListMarker = new ArrayList<>();
public String category;
public String mood;
public String name;
public int i;
public String desc;
public int count = 0;
//private static final String insertURL = "http://192.168.0.26/php2/UpdateCount.php";
//private static final String TAG_SUCCESS = "success";
String DataParseUrl = "http://192.168.254.105/php2/UpdateCount.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    assert mapFragment != null;
    mapFragment.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    getAllDataLocationLatLng();
}

private void getAllDataLocationLatLng() {
    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setMessage("Loading Marker ..");
    dialog.show();
    Intent i = getIntent();
    category = i.getExtras().getString("category");
    mood = i.getExtras().getString("mood");
    ApiService apiService = ApiClient.getClient().create(ApiService.class);
    Call<ListLocationModel> call = apiService.getAllLocation("JsonDisplayMarker.php?category=" + category + "&mood=" + mood);
    call.enqueue(new Callback<ListLocationModel>() {
        @Override
        public void onResponse(Call<ListLocationModel> call, Response<ListLocationModel> response) {
            dialog.dismiss();
            mListMarker = response.body().getmData();
            initMarker(mListMarker);
        }

        @Override
        public void onFailure(Call<ListLocationModel> call, Throwable t) {
            dialog.dismiss();
            Toast.makeText(MapsActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

}

private void initMarker(final List<LocationModel> listData) {
    for (i = 0; i < mListMarker.size(); i++) {
        //requestQueue = Volley.newRequestQueue(this.getApplicationContext());
        name = mListMarker.get(i).getImageLocationName().trim();
        desc = mListMarker.get(i).getDescription();
        LatLng location = new LatLng(Double.parseDouble(mListMarker.get(i).getLatutide()), Double.parseDouble(mListMarker.get(i).getLongitude()));
        mMap.addMarker(new MarkerOptions().position(location).title(name).snippet(desc));
        LatLng latLng = new LatLng(Double.parseDouble(mListMarker.get(0).getLatutide()), Double.parseDouble(mListMarker.get(0).getLongitude()));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latLng.latitude, latLng.longitude), 16));
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Intent intent = new Intent(MapsActivity.this, DetailsActivity.class);
                intent.putExtra("namelocation", marker.getTitle());
                intent.putExtra("description", marker.getSnippet());
                intent.putExtra("count", String.valueOf(count++));
                //SendDataToServer(marker);
                class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
                    @Override
                    protected String doInBackground(String... strings) {

  //HERE IS THE PASSING OF THE DATA

                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("namelocation",name));
                        nameValuePairs.add(new BasicNameValuePair("count",String.valueOf(count)));
                        try {
                            HttpClient httpClient = new DefaultHttpClient();

                            HttpPost httpPost = new HttpPost(DataParseUrl);

                            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                            HttpResponse response = httpClient.execute(httpPost);

                            HttpEntity entity = response.getEntity();


                        } catch (ClientProtocolException e) {

                        } catch (IOException e) {

                        }
                        return "Data Submit Successfully";
                    }

                    @Override
                    protected void onPostExecute(String result) {
                        super.onPostExecute(result);
                        Toast.makeText(MapsActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();
                    }
                }
                SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
                sendPostReqAsyncTask.execute(name, String.valueOf(count));
                startActivity(intent);
            }

            });


        }
    }

}

1 个答案:

答案 0 :(得分:0)

在您的php脚本更改中

$namelocation = $_GET['namelocation'];
$count = $_GET['count'];

$namelocation = $_REQUEST['namelocation'];
$count = $_REQUEST['count'];