java - Data Insertion error while trying to insert a json array? -
i trying send json array mysql data base using google's volley library. following type of data 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 data insertion error :-
data insertion erroryou have error in sql syntax; check manual corresponds mysql server version right syntax use near 'key,dbname,status,role)values( '', 'anup borde', '8600931386', '1223', 'test sho' @ line 1
i trying run 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 $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 goldmine_users (name,mobile,password,role)values('anup','8600931386','123','admin'); $query="insert 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 volley code send data 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 how database looks like:-
any or suggestion welcome.i think doing silly mistake somewhere great if guide me correct solution.
$query="insert goldmine_users(name,mobile,password,shop_name,area,city,vatno,cstno,key,dbname,status,role)values".$values;
key reserved keyword , should therefor escaped in backticks (or similar)
$query="insert goldmine_users(`name`,`mobile`,`password`,`shop_name`,`area`,`city`,`vatno`,`cstno`,`key`,`dbname`,`status`,`role`)values".$values;
Comments
Post a Comment