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);
    ?>
1
  • 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. Commented Mar 8, 2018 at 14:15

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
yes , you are right and i have modified the answer a bit, thanks sir
Yes you are ryt how can i send max[0],max[1] and max[2] to s1 variable of my php
@StudioMaster you need to create a jsonarray request and this is how you can send your array as jsonarray
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);
    }
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.