newBieUser0829 newBieUser0829 - 3 months ago 28
MySQL Question

How to Insert Data to Mysql Database using Volley

I am trying to insert data to mysql database using volley, but it always response as error, I'm stuck and cant go further to my project. I can't find what causing it not to execute the my PHP codes. I also checked my PHP code using PostMan and it is working well. I'm now trying to insert some test data to it but still no luck. I hope you can shine some light in me and provide me some help. Thank you.

Insert.Java

Button confirmOrder = (Button) findViewById(R.id.confirmOrder);
confirmOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

final int a = 1;
final String b = "Beth";
final int c = 1;
final int d = 3;
final int e = 1;
final String f = "Bert";



final String URL = "https://10.0.2.2/myDB/order.php";

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.contains("success")) {
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"failed",Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("table_id", String.valueOf(a));
params.put("cust_name", b);
params.put("item_id", String.valueOf(c));
params.put("quantity", String.valueOf(d));
params.put("status_id", String.valueOf(e));
params.put("username", f);
return params;
}
};

MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

}
});


order.php

<?php
include_once("init.php");

if(isset($_POST['table_id']) && isset($_POST['cust_name']) &&
isset($_POST['item_id']) && isset($_POST['quantity']) && isset($_POST['status_id'])
&& isset($_POST['username'])){
$tableid = $_POST['table_id'];
$custname = $_POST['cust_name'];
$itemid = $_POST['item_id'];
$quantity = $_POST['quantity'];
$statusid = $_POST['status_id'];
$username = $_POST['username'];

$query = "INSERT INTO tbl_order(table_id,cust_name,item_id,quantity,status_id,username)
VALUES ('$tableid', '$custname', '$itemid', '$quantity', '$statusid', '$username')";

$result = mysqli_query($con, $query);

if($result > 0){
echo "success";
}
else{
echo "failed";
}
}


?>

Answer

The line - final String URL = "https://10.0.2.2/myDB/order.php";

http://10.0.2.2/myDB/order.php"; you misspelled http