如何在php中读取json数组
我试图用php读取android数据并将其插入两个不同的表中(一个表包含公共数据,另一个表包含数组中的每个项)。将公共数据插入orders表可以正常工作,但数组数据不能 以下是我的android代码和我的basicNameValuePairs:如何在php中读取json数组,php,android,arrays,json,Php,Android,Arrays,Json,我试图用php读取android数据并将其插入两个不同的表中(一个表包含公共数据,另一个表包含数组中的每个项)。将公共数据插入orders表可以正常工作,但数组数据不能 以下是我的android代码和我的basicNameValuePairs: ArrayList<ShoppingCardArticle> shoppingCardArticles = UsingSharedPrefs.getShoppingCard(); List<NameValueP
ArrayList<ShoppingCardArticle> shoppingCardArticles = UsingSharedPrefs.getShoppingCard();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
TextView time = (TextView)findViewById(R.id.textView1);
String stringTime = time.getText().toString();
int amount = shoppingCardArticles.size();
String sAmount = Integer.toString(amount);
double fullprice = 0;
for(int a=0;a<shoppingCardArticles.size();a++) {
double price = shoppingCardArticles.get(a).getArticlePrice();
int quant = shoppingCardArticles.get(a).getArticleQuantity();
price = price * quant;
fullprice = fullprice + price;
}
String sFullprice = Double.toString(fullprice);
nameValuePairs.add(new BasicNameValuePair("fullprice", sFullprice));
nameValuePairs.add(new BasicNameValuePair("amount", sAmount));
nameValuePairs.add(new BasicNameValuePair("time", stringTime));
for(int i = 0; i<shoppingCardArticles.size(); i++) {
String proid = Integer.toString(shoppingCardArticles.get(i).getArticleId());
String proquan = Integer.toString(shoppingCardArticles.get(i).getArticleQuantity());
nameValuePairs.add(new BasicNameValuePair("proid[]", proid));
nameValuePairs.add(new BasicNameValuePair("proquan[]", proquan));
}
JSONParser jsonParser = new JSONParser();
JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", nameValuePairs);
return null;
ArrayList shoppingCardArticles=使用SharedPrefs.getShoppingCard();
List nameValuePairs=新的ArrayList();
TextView时间=(TextView)findViewById(R.id.textView1);
String stringTime=time.getText().toString();
int amount=shoppingcardcarticles.size();
字符串sAmount=Integer.toString(金额);
双倍全价=0;
对于(INTA=0;误差;
}
$proID=array($_POST['proID']);
$proQuan=数组($_POST['proQuan']);
foreach($proID作为$item){
$productid=$item['proid'];
$productquantity=$item['proquan'];
$query=“插入订单产品(ORDD、proID、proQuan)值(“$ordDynID”、“$productid”、“$productquantity”)”;
if($mysqli->query($query)==TRUE){
echo“新记录创建成功”;
}否则{
echo“Error:”.$query.“
”$mysqli->Error;
}
}
$mysqli->close();
所以我的问题是,我在读取php文件中的数据时是做错了什么,还是android代码中有问题
提前谢谢
干杯我不是android开发人员,但我可以在PHP代码中帮你调试。首先,你能做一个var_dump($_POST)并告诉我输出吗 我可以看出您使用的json_decode是错误的,看起来您试图对每个键值对使用它,而不是完整的json文本。 它的工作方式(检查是您向它传递一个完整的json文本(即“{”a“:1,“b“:2}”),并将其转换为可以通过这种方式访问的POPO(普通旧PHP对象)
$object = json_decode('{ "a":1, "b":2 }');
$a = $object->a; // $a = 1
$b = $object->b; // $b = 2
当您在Android代码中按名称发布数组时,如
proID[]
和proQuan[]
。$\u post['proID']
和$\u post['proQuan']
已经是数组了。这类似于html表单,它有多个带有方括号名称的输入。(我不是android开发者,但我可以在PHP代码中帮你调试。首先,你能做一个var_dump($\u POST)并告诉我输出吗?也许你应该在proid[]
和proquan[]
中为每个条目提供索引。要访问PHP中的http POST数组,请使用$\u POST['proid']
和$\u POST['proquan']
。不需要方括号。谢谢你的回答!这是我的输出:array(5){[“fullprice”]=>string(3)“4.1”[“amount”]=>string(1)“2”[“time”]=>string(5)”:-“[“proid”]=>array(2){[0]=>string(1)“3”}[“proquan”]=>array(2){[0]=>string(1)“1”=>string(1)”“1”}
好了,您根本不需要使用json\u解码,我相信Android的jsonParser会帮您解决这个问题。只需执行$fullPrice=$\u POST['fullPrice']等等。请小心,虽然在将输入传递到数据库之前,您看起来并没有对其进行清理,这可能会导致SQLInjection漏洞,但您可能希望了解这一点以及如何防止itI使其工作,非常感谢:)!我真的必须检查漏洞!它确实来自阵列($\u POST…)行!已删除阵列,它正在工作:)。
$object = json_decode('{ "a":1, "b":2 }');
$a = $object->a; // $a = 1
$b = $object->b; // $b = 2
$proID = $_POST['proid'];
$proQuan = $_POST['proquan'];
for ($i = 0; $i < count($proID); $i++) {
$productid = $proID[$i];
$productquantity = $proQuan[$i];
//your insert goes here
}