0

I am trying to send following data to my php but program crashes if I put more than one s1 variable.

Java code:

    //my java code goes here
        @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
            params.put("s1",max[0]);
            params.put("s1",max[1]);
            params.put("s1",max[3]);
        return params;
    }

PHP code:

    //my php code goes here
        <?php
     $con = mysqli_connect('localhost','myuser','mypass');
     mysqli_select_db($con,'mydb');

     $checked = $_POST["s1"];

     $SQL = "INSERT INTO TABLE VALUES ('$checked[0]','$checked[1]','$checked[2]')";
     mysqli_query($con,$SQL);
    ?>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Try outputting `$checked` in PHP and seeing exactly what it gets. Put it here if still unsure, and this will help answer your question. Also: your PHP code is vulnerable to SQL injections. Look up prepared statements. – Ynhockey Mar 08 '18 at 14:15

3 Answers3

4

Map can only store one key and one value, duplicate keys are not allowed so in short you are just sending a single value and trying to fetch multiple values using index (which does not exists) hence the exception

Solution : Either use different keys and fetch values using those keys on server or send the whole array

To send whole array , simply Create JSONObject or JSONArray request instead of String

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • 1
    A whole array won't work here- these are GET http parameters. There's no built in way to send an array. He could make them part of one comma deliminated string and parse it on the server side, but to send an array he'd need to send it as a body. – Gabe Sechan Mar 08 '18 at 14:18
  • yes , you are right and i have modified the answer a bit, thanks sir – Pavneet_Singh Mar 08 '18 at 14:22
  • Yes you are ryt how can i send max[0],max[1] and max[2] to s1 variable of my php – Studio Master Mar 08 '18 at 14:27
  • @StudioMaster you need to create a jsonarray request and [this is how you can send your array as jsonarray](https://stackoverflow.com/a/5166890/4936904) – Pavneet_Singh Mar 08 '18 at 14:44
0

There is my example:

public void todo(String value, String value2)
    {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://adress.com/script.php", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //response
                Log.d("Response", response);

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        error.printStackTrace();
                    }
                }
        ){
            @Override
            protected Map<String, String> getParams()
            {


                Map<String, String> params = new HashMap<String, String>();
                params.put("s1", value1);
                params.put("s2", value2);

                return params;
            }
        };
        MySingleton.getInstance(this).addToRequestque(stringRequest);
    }

MySingleton.java :

public class MySingleton {

    private  static MySingleton mInstance;
    private RequestQueue requestQueue;
    private static Context mCtx;
    private MySingleton(Context context)
    {
        mCtx = context;
        requestQueue = getRequestQueue();
    }

    public RequestQueue getRequestQueue()
    {
        if(requestQueue==null)
        {
            requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return requestQueue;
    }

    public static synchronized MySingleton getInstance(Context context)
    {

        if (mInstance==null)
        {
            mInstance = new MySingleton(context);
        }
        return mInstance;

    }

    public <T>void addToRequestque(Request<T> request)
    {
        requestQueue.add(request);
    }
}
Peter Valek
  • 87
  • 1
  • 2
  • 11
0

I assume this is generating a query string like

?s1=A&s1=B&s1=C

This will result in each value of s1 overwriting the last and s1 will contain 'C', not an array.

If you want s1 to be an array you need to use s1[] as the label, so it will generate a query string like

?s1[]=A&s1[]=B&s1[]=C

So then your Java would be:

  params.put("s1[]",max[0]);
  params.put("s1[]",max[1]);
  params.put("s1[]",max[3]);

BUT, and this is REALLY IMPORTANT, you are not escaping the posted values before you put them opening yourself up to SQL injection attacks if your PHP page is in any way open to the public (and even if it isn't you should still guard against this) Have a look at http://php.net/manual/en/mysqli.real-escape-string.php

james-geldart
  • 709
  • 7
  • 9