简体   繁体   中英

Volley Library giving error 500 on post request

I have the following code that I am using to save data into a mysql database;

    private void save(final String orderId, final String client, final String name, final String Seller, final String amount, final String quantity, final double longi, final double lat, final String location, final ProgressDialog progressDialog) {
    String URL_ORDER = "https://foodfuzz.co.ke/foodfuzzbackend/market/orders/order.php";
    StringRequest orderStringRequest = new StringRequest(Request.Method.POST, URL_ORDER,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject orderObject = new JSONObject(response);
                        String orderSuccess = orderObject.getString("success");
                        if(orderSuccess.equals("1")){
                            Toast.makeText(CheckOutActivity.this,"Order Placed Successfully " , Toast.LENGTH_SHORT).show();
                            pay.setVisibility(View.GONE);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        progressDialog.dismiss();
                        Toast.makeText(CheckOutActivity.this,"Unable to place order " + e.toString(), Toast.LENGTH_SHORT).show();
                        pay.setEnabled(true);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(CheckOutActivity.this,"Error placing order " + error.toString(), Toast.LENGTH_SHORT).show();
                    pay.setEnabled(true);
                }
            }){
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("orderId",orderId);
            params.put("name", name);
            params.put("client", client);
            params.put("seller", Seller);
            params.put("amount", amount);
            params.put("quantity",quantity);
            params.put("longitude",String.valueOf(longi));
            params.put("latitude",String.valueOf(lat));
            params.put("location",location);
            return params;
        }
    };
    RequestQueue orderRequestQueue = Volley.newRequestQueue(this);
    orderRequestQueue.add(orderStringRequest);

}

My problem is that I get the error code 500 when I make a post request. The following is the actual error logged on logcat

BasicNetwork.performRequest: Unexpected response code 500 for https://foodfuzz.co.ke/foodfuzzbackend/market/orders/order.php

This is the php code that is on the server at the stated url

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    require_once '../db/connector.php';
    $orderId = mysqli_real_escape_string($conn, $_POST['orderId']);
    $client = mysqli_real_escape_string($conn, $_POST['client']);
    $product = mysqli_real_escape_string($conn, $_POST['name']);
    $seller = mysqli_real_escape_string($conn, $_POST['seller']);
    $amount = mysqli_real_escape_string($conn, $_POST['amount']);
    $quantity = mysqli_real_escape_string($conn, $_POST['quantity']);
    $longi = mysqli_real_escape_string($conn, $_POST['longitude']);
    $lati = mysqli_real_escape_string($conn, $_POST['latitude']);
    $loc = mysqli_real_escape_string($conn, $_post['location']);
    $status = 1;

    $sql = "INSERT INTO tbl_orders (orderid, client, product, seller, amount, quantity, longitude, latitude, deliveryloc, status) VALUES ('$orderId', '$client', '$product', '$seller', '$amount', '$quantity', '$longi', '$lati', '$loc', '$status')";

    if (mysqli_query($conn, $sql)) {
        $result["success"] = "1";
        $result["message"] = "success";

        echo json_encode($result);
        mysqli_close($conn);
    } else {

        $result["success"] = "0";
        $result["message"] = "error";

        echo json_encode($result);
        mysqli_close($conn);
    }
}

The code works on postman without any error. What could I be doing wrong

This error is normally caused by errors in your code, first it is good to check if your php script is error free and the other codes as well. In my case the post data was not matching the table columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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