简体   繁体   English

Volley 库在发布请求时给出错误 500

[英]Volley Library giving error 500 on post request

I have the following code that I am using to save data into a mysql database;我有以下代码用于将数据保存到 mysql 数据库中;

    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.我的问题是当我发出发布请求时收到错误代码 500。 The following is the actual error logged on logcat以下是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这是 url 服务器上的 php 代码

<?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.该代码适用于 postman 没有任何错误。 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.此错误通常是由您的代码中的错误引起的,首先最好检查您的 php 脚本是否无错误以及其他代码是否正确。 In my case the post data was not matching the table columns.在我的情况下,发布数据与表格列不匹配。

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

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