I want to send data to the database.My PHP Files are OK,because I had tested that with postman and it send data to the database.But when I used Android app for that it shouldn't send data to the database.Here is my java method for the sending data.Reply me with the solution please.
public void insertData(String name,String category,int quantity){
String url = "http://192.168.172.2/foods/insert.php";
try {
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(name, "UTF-8");
data += "&" + URLEncoder.encode("category", "UTF-8") + "="
+ URLEncoder.encode(category, "UTF-8");
data += "&" + URLEncoder.encode("quantity", "UTF-8")
+ "=" + URLEncoder.encode(String.valueOf(quantity), "UTF-8");
URL url1 = new URL(url);
HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(data);
writer.flush();
Log.d("datainsert","success"+data);
} catch (MalformedURLException e) {
Log.d("datainsert","Error"+e.getMessage());
} catch (IOException e) {
Log.d("datainsert","Error"+e.getMessage());
}
<?PHP
require "config.php";
global $connect;
if (isset($_POST['name']) and isset($_POST['category']) and isset($_POST['quantity'])) {
$name = $_POST['name'];
$category = $_POST['category'];
$quantity = $_POST['quantity'];
$intq = (int)$quantity;
if ($_SERVER['REQUEST_METHOD']== "POST") {
$query = "INSERT INTO food_details(food_name,category,quantity) VALUES('$name','$category',$intq);";
if($q = mysqli_query($connect,$query)){
echo "successfully inserted";
}else{
//echo mysqli_error(mysqli_query($connect,$query));
echo "not success";
echo "Error: " . $query . "<br>" . $connect->error;
}
}
}
mysqli_close($connect);
?>