簡體   English   中英

來自nameValuePairs的值未成功傳遞到數據庫

[英]Value from nameValuePairs not successfully passed to database

我在將數據傳遞到數據庫時使用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);
            }

            });


        }
    }

}

在您的PHP腳本更改

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM