0

Hello developers out there,

I want a php file communicate between android application and mysql server. Because I had alot of trouble setting this up, I tried to learn it step by step because none of the tutorials I found so far is working for me. After a while I understod that my php file does not get any _POST response.

This is the php file:

<?php
if(isset($_POST['id'])){
    echo $_POST['id'];
}else{
    echo "no id";
}
if(isset($_POST['stringdata'])){
    echo $_POST['stringdata'];
}else{
    echo "no stringdata";
}
?>

which always shows "no idno stringdata" (tested with postman and other files also tested with android studio). So in every file this "isset($_POST['stringdata']))" gives false.

Now my question: what am I doing wrong? How can I give variables to the php file? Thank you :)

EDIT:

How I said I am doing it with "Postman", this programm gives this line to submit it if I am getting it right:

localhost/android_connect/testscript.php?id=123&stringdata=whatever

Pumpanickel
  • 91
  • 2
  • 10
  • 3
    Show us the code you are using to submit this. You probably aren't posting anything with the name being `stringdata` – GrumpyCrouton Oct 15 '18 at 20:08
  • Try `print_r($_POST)` and show us what you have. – nerdlyist Oct 15 '18 at 20:08
  • Also make sure you are submitting a post request not a get... – nerdlyist Oct 15 '18 at 20:08
  • Post variables are submitted through a form – Bobby Axe Oct 15 '18 at 20:09
  • 2
    @BobbyAxe that is not 100% accurate. You can submit a post in many ways as well as a get. – nerdlyist Oct 15 '18 at 20:10
  • @BobbyAxe there is no way to populate `$_POST` variable with a GET request but if you set your form like `method= "GET"` it is a get. If you curl data using POST it is post. The OP said he is using Postman which lets you choose your method `GET, POST, PUT, PATCH...` the list is long. Point is it being a form is irreverent. – nerdlyist Oct 15 '18 at 20:14
  • print_r($_POST) gives me: Array (), and I am making it with POST – Pumpanickel Oct 15 '18 at 20:19
  • @nerdlyist please read the question again to understand my comment. Yes a form is not the only way, but consider this line `I want a php file communicate between android application and mysql server` – Bobby Axe Oct 15 '18 at 20:20
  • And to understand it more clear: I just wrote the introduction to get my overall goal, but for now i just want the make sure that my php script it getting this POST right – Pumpanickel Oct 15 '18 at 20:22
  • @BobbyAxe this would help my point that a form is irreverent this should be an API call on POST. – nerdlyist Oct 15 '18 at 20:28
  • 1
    Possible duplicate of [When should I use GET or POST method? What's the difference between them?](https://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them) – miken32 Oct 15 '18 at 20:35
  • @nerdlyist i believed that op was using postman for testing purposes not as part of main project but if that not be the case then your answer should be perfect for op's question – Bobby Axe Oct 15 '18 at 20:35
  • @BobbyAxe and nerdlyist: Maybe you guys could answer my new problem posted some minutes ago, I would be so glad: https://stackoverflow.com/questions/52843267/insert-data-into-mysql-db-with-php-script-doesnt-work – Pumpanickel Oct 16 '18 at 20:07

1 Answers1

3

You need to submit a POST request in Postman

The uri localhost/android_connect/testscript.php?id=123&stringdata=whatever is submitting query paramas which are for a GET request.

There are couple solutions:

Use $_GET:

<?php
if(isset($_GET['id'])){
    echo $_GET['id'];
}else{
    echo "no id";
}
//... other code
?>

Be lazy with $_REQUESTthis one check both $_POST and $_GET but I would not recommend it.

<?php
if(isset($_REQUEST['id'])){
    echo $_REQUEST['id'];
}else{
    echo "no id";
}
//... other code
?>

Update you call to be a POSTand then set the data to send and leave your code as is.

Change Method

nerdlyist
  • 2,842
  • 2
  • 20
  • 32