2

I am trying to send a json array to mysql data base using google's volley library. The following is the type of data I am sending:-

[{"vatno":"","area":"Testing bay","status":"0","cstno":"","dbname":"webappdb","name":"ANUP BORDE","emp_role":"Manager","password":"1223","key":"","city":"Pune","shop_name":"Test Shop","mobile":"8600931386"}]

I get a data insertion error something like :-

Data insertion errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key,dbname,status,role)VALUES( '', 'ANUP BORDE', '8600931386', '1223', 'Test Sho' at line 1

I am trying to run this php script.

<?php

        include "config.php";
        $dbname ="goldmine";
        $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
         if(!$con)
         {
           echo "Connection Error".mysqli_connect_error();
         }
         else{
        //echo "";
         }

        $arrAssoc = json_decode(stripslashes($_POST['jsonarray']), true);
        $values="";
        foreach($arrAssoc as $aa){
                $values.="(";
                $values.=" '".$aa['']."',";
                $values.=" '".$aa['name']."',";
                $values.=" '".$aa['mobile']."',";
                $values.=" '".$aa['password']."',";
                $values.=" '".$aa['shop_name']."',";
                $values.=" '".$aa['area']."',";
                $values.=" '".$aa['city']."',";
                $values.=" '".$aa['vatno']."',";
                $values.=" '".$aa['cstno']."',";
                $values.=" '".$aa['key']."',";
                $values.=" '".$aa['dbname']."',";
                $values.=" '".$aa['status']."',";
                $values.=" '".$aa['role']."'";
                $values.="),";
        }
        $values=rtrim($values,',');
                //Insert into goldmine_users (name,mobile,password,role)Values('Anup','8600931386','123','admin');

        $query="INSERT INTO goldmine_users(name,mobile,password,shop_name,area,city,vatno,cstno,key,dbname,status,role)VALUES".$values;

        if(mysqli_query($con,$query))
        {
        echo "Data inserted";
        }else
        {
       echo "Data insertion error".mysqli_error($con);
        }


?>

And this is my volley code to send the data to mysql database.

public void registerUser()
{
    final String jsonArray = rolejson.toString().trim();
    Log.d("JSON RESPONSE",jsonArray);
    StringRequest stringReq = new StringRequest(Request.Method.POST, REGISTERUSER, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            Log.d("USER INFO RESPONSE",response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Log.d("LOG_USERINFO", jsonArray);
            Map<String, String> params = new HashMap<>();
            params.put(KEY_JSONARRAY, jsonArray);
            return params;
        }
    };

    RequestQueue rq = Volley.newRequestQueue(this);
    rq.add(stringReq);
}

This is how my database looks like:- enter image description here

Any help or suggestion is welcome.I think I am doing a silly mistake somewhere it would be great if someone would guide me to the correct solution.

2
  • "key" is a reserved keyword for most database implementations. Did you make sure to escape it in back ticks or something similar? Commented Apr 25, 2016 at 11:07
  • Thank you so much @911DidBush.you solved my problem Commented Apr 25, 2016 at 11:21

2 Answers 2

2
 $query="INSERT INTO goldmine_users(name,mobile,password,shop_name,area,city,vatno,cstno,key,dbname,status,role)VALUES".$values;

key is a reserved keyword and should therefor be escaped in backticks (or something similar)

 $query="INSERT INTO goldmine_users(`name`,`mobile`,`password`,`shop_name`,`area`,`city`,`vatno`,`cstno`,`key`,`dbname`,`status`,`role`)VALUES".$values;
Sign up to request clarification or add additional context in comments.

Comments

1

Your amount of columns and the data you want to insert doesn't match. You're trying to insert data in 12 columns however you deliver 13 columns of data:

            $values.="(";
            $values.=" '".$aa['']."',"; //Remove this line
            $values.=" '".$aa['name']."',";
            $values.=" '".$aa['mobile']."',";
            $values.=" '".$aa['password']."',";
            $values.=" '".$aa['shop_name']."',";
            $values.=" '".$aa['area']."',";
            $values.=" '".$aa['city']."',";
            $values.=" '".$aa['vatno']."',";
            $values.=" '".$aa['cstno']."',";
            $values.=" '".$aa['key']."',";
            $values.=" '".$aa['dbname']."',";
            $values.=" '".$aa['status']."',";
            $values.=" '".$aa['role']."'";
            $values.="),";

//12 columns

$query="INSERT INTO goldmine_users(name,mobile,password,shop_name,area,city,vatno,cstno,key,dbname,status,role)VALUES".$values;

4 Comments

I did remove that line it doesn't work it gives me the same error.
Can you do an echo $values; after the foreach, and post the result in your question?
@user3295583 deleted my answer as Daan has suggested same thing.
Rohit I cant delete your answer.You will have to delete your answer.Thank you anyways.

Your Answer

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