• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Can't insert data to mysql database from android app

oriz

Newbie
Feb 17, 2014
46
1
Hey I am trying to insert data to my sql data base and I cant figure out why it isn't working, when I run the php script on the host alone its working but when I try to do it from the android app it just doesn't do nothing I couldn't find the problem so I thought you guys might help me

p.s its not a connection problem I checked the connection and its fine.

here it the android code:

  1. @override
  2. protected String doInBackground(String... params) {

  3. String insertUrl = "http://localhost/insert.php";

  4. club_name = params[1];
  5. String publisher_name = params[2];
  6. String club_contact = params[3];
  7. String club_location = params[4];
  8. String club_hours = params[5];
  9. String club_age = params[6];
  10. String club_description = params[7];

  11. try {
  12. URL url = new URL(insertUrl);
  13. HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
  14. httpURLConnection.setRequestMethod("POST");
  15. httpURLConnection.setDoOutput(true);
  16. OutputStream outputStream = httpURLConnection.getOutputStream();
  17. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
  18. String data = URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(club_name,"UTF-8")+"&"+
  19. URLEncoder.encode("number","UTF-8")+"="+URLEncoder.encode(publisher_name,"UTF-8");
  20. // URLEncoder.encode("publisher_phone","UTF-8")+"="+URLEncoder.encode(club_contact,"UTF-8")+"&"+
  21. // URLEncoder.encode("location","UTF-8")+"="+URLEncoder.encode(club_location,"UTF-8")+"&"+
  22. // URLEncoder.encode("hour_open","UTF-8")+"="+URLEncoder.encode(club_hours,"UTF-8")+"&"+
  23. // URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(club_age,"UTF-8")+"&"+
  24. // URLEncoder.encode("description","UTF-8")+"="+URLEncoder.encode(club_description,"UTF-8");
  25. bufferedWriter.write(data);
  26. bufferedWriter.flush();
  27. bufferedWriter.close();
  28. outputStream.close();
  29. InputStream inputStream = httpURLConnection.getInputStream();
  30. inputStream.close();



  31. } catch (MalformedURLException e) {
  32. e.printStackTrace();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }

  36. return "Party has been posted!";
  37. }
and the php file:

  1. <?php
  2. require "connection.php";

  3. $club_name = $_POST["club_name"];
  4. $publisher_name = $_POST["publisher_name"];
  5. $publisher_phone = $_POST["publisher_phone"];
  6. $location = $_POST["location"];
  7. $hour_open = $_POST["hour_open"];
  8. $age = $_POST["age"];
  9. $description = $_POST["description"];

  10. $sql = "insert into apps(club_name,publisher_name,publisher_phone,location,hour_open,age,description) values ('$club_name','$publisher_name','$publisher_phone','$location','$hour_open','$age','$description')";
  11. $query = mysqli_query($conn,$sql);

  12. ?>
  13. connection php:

  14. <?php
  15. $db_name = "ori";
  16. $mysql_username = "root";
  17. $mysql_password = "";
  18. $server_name = "localhost";
  19. $conn = mysqli_connect($server_name,$mysql_username,$mysql_password,$db_name);

  20. if($conn)
  21. {
  22. //echo "connects";
  23. }
  24. ?>
 
Try sending a json object not a output stream. You php is parsing a json object from the looks of it.

Why do you say he's using json? All that's happening is encoded parameters are being added to the URL, which are then accessed by the server side PHP code.

And btw the only parameters being set are "age" and "number", which incidentally appear to be using completely the wrong values.

Code:
String data = URLEncoder.encode("age","UTF-8")+"="+URLEncoder.encode(club_name,"UTF-8")+"&"+
URLEncoder.encode("number","UTF-8")+"="+URLEncoder.encode(publisher_name,"UTF-8");

I think the OP probably meant to write

Code:
String data = URLEncoder.encode("club_name","UTF-8")+"="+URLEncoder.encode(club_name,"UTF-8")+"&"+
URLEncoder.encode("publisher_name","UTF-8")+"="+URLEncoder.encode(publisher_name,"UTF-8");

So the php code tries to extract a load of POST parameters, most of which are missing. It could be that the insert statement fails because some fields on the table are not nullable. Without seeing the table definition, or error log I couldn't say for sure.

OP should also consult examples of how to construct a HTTP POST request, with URL encoded parameters, such as:

http://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post
 
Last edited by a moderator:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones