简体   繁体   English

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

[英]Value from nameValuePairs not successfully passed to database

I am using nameValuePairs in passing my datas to the database. 我在将数据传递到数据库时使用nameValuePairs。 The values inside it are from a map. 其中的值来自地图。 I put it into a string then put the string to the nameValuePairs. 我将其放入字符串中,然后将字符串放入nameValuePairs中。 My php seems to work fine. 我的PHP似乎工作正常。 My goal here is to update my table containing counts using the where inside my SQL statement. 我的目标是使用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"; 
}
?>

This is my Activity in passing the datas using nameValuePairs. 这是我使用nameValuePairs传递数据的活动。

MapsActivity.java 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);
            }

            });


        }
    }

}

in your php script change 在您的PHP脚本更改

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM