嗨,我是新来的人,在我的android编程中有些卡住了,我无法使用php将购物车列表中的数据插入数据库。
pro.php
<?php require_once 'connect1.php'; if ($_SERVER['REQUEST_METHOD'] =='POST'){
$products = $_POST['products'];
$decoded = json_decode($products, true);
for($i = 0; $i < sizeof($decoded); $i = $i + 1) {
$sql = "INSERT INTO product_table (product, price) VALUES (\"".$decoded[$i]['product']."\", ".$decoded[$i]['price'] .")";
if ( mysqli_query($conn, $sql) ) {
$result["success"] = "1";
$result["message"] = "success";
} else {
$result["success"] = "0";
$result["message"] = mysqli_error($conn);
break;
}
mysqli_close($conn);
}
echo json_encode($result);}?>
我的json: krnjfrag.class
private void placeorder() {
final ProgressDialog pDial = new ProgressDialog(getActivity());
pDial.setMessage("Loading...");
pDial.show();
Map<String,String> params = new HashMap<>();
params.put("product", String.valueOf(title));
params.put("price", String.valueOf(price));
JSONObject parameters = new JSONObject();
JSONArray products = new JSONArray();
for(Product p:example){
JSONObject prod = new JSONObject();
try {
prod.put("product",p.getTitle());
prod.put("price",p.getPrice());
products.put(prod);
parameters.put("products",products);
} catch (JSONException e) {
e.printStackTrace();
}
}
JsonObjectRequest keranjang = new JsonObjectRequest(Request.Method.POST,URL_pesan,parameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
System.out.println();
try{
String success = response.getString("success");
/*Intent intent = new Intent(getActivity(),CheckfinalActivity.class);
intent.putExtra("product", title);
intent.putExtra("price",price);
startActivity(intent);
String title = response.getString("product");
String price = response.getString("price");*/
Toast.makeText(getActivity(), "Tersimpan", Toast.LENGTH_SHORT).show();
madapter.notifyDataSetChanged();
pDial.dismiss();
/*Product product = new Product();
product.setTitle(jsonObject.getString("product"));
product.setPrice(jsonObject.getDouble("price"));
example.add(product);
total.setText((int) jsonObject.getDouble("total"));*/
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Gagal", Toast.LENGTH_SHORT).show();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(getActivity(), "Tidak dapat menyimpan", Toast.LENGTH_SHORT).show();
pDial.dismiss();
}
});
Volley.newRequestQueue(getActivity()).add(keranjang);
}
我只希望可以将recyclerview上的数据插入数据库,因此,如果我在管理员端打开,我可以知道用户想购买哪个。
我总是在解析错误时出错