1. Are you ready for the Galaxy S20? Here is everything we know so far!

problems inserting into online database

Discussion in 'Android Development' started by jonbonazza, Aug 18, 2010.

  1. jonbonazza

    jonbonazza Android Expert
    Thread Starter

    ok, so an app I am working on needs to read from an online database and display the results dynamically. This works fine, but then when I press a button in my UI, I need it to add a new row into the same database. When I test the app and click the button, it doesn't throw any exceptions or anything and once it finsihes the process goes right back to letting me do w/e I need on the UI. When I test the PHP script through the browser, it goes through all he echos I have in the script and reaches the bottom of the script no problem, however it doesn't add the row... I am at a loss here... Maybe you all can help.

    Here is my PHP file:
    Code (Text):
    1.  
    2. <?php
    3. mysql_connect("localhost","XXXXX","XXXXX");
    4. mysql_select_db("bonafie0_mm");
    5. echo "connected to database";
    6. mysql_query("INSERT INTO Comments (user, comment) VALUES ('".$_REQUEST['user']."', ".$_REQUEST['comment']."')");
    7. echo "done. :D";
    8. mysql_close();
    9. ?>
    10.  
    and here is my android code:
    Code (Text):
    1.  
    2. private void postComment()
    3.     {
    4.         String user = editName.getText().toString();
    5.         String comment = editComment.getText().toString();
    6.         OutputStream os = null;
    7.         ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
    8.         nameValuePair.add(new BasicNameValuePair("user", user));
    9.         nameValuePair.add(new BasicNameValuePair("comment", comment));
    10.         try
    11.         {
    12.             HttpClient client = new DefaultHttpClient();
    13.             HttpPost post = new HttpPost(ROTM_POST_URL);
    14.             post.setEntity(new UrlEncodedFormEntity(nameValuePair));
    15.             HttpResponse response = client.execute(post);
    16.            
    17.         }
    18.         catch(IOException e)
    19.         {
    20.             Toast.makeText(RoTM.this, "Unable to post comments", Toast.LENGTH_SHORT).show();
    21.         }
    22.         editName.setText("");
    23.         editComment.setText("");
    24.     }
    25.  
    Any ideas?
     


  2. jonbonazza

    jonbonazza Android Expert
    Thread Starter

    Fixed it. Combination of a typo in the PHP file and a typo in the java code.
     
  3. rick99gtp

    rick99gtp Newbie

    Can you post the correct code so I can understand what you did?
     
  4. jonbonazza

    jonbonazza Android Expert
    Thread Starter

    Sure. Here is the correct code:

    PHP File:
    Code (Text):
    1.  
    2. <?php
    3. mysql_connect("localhost","username","password");
    4. mysql_select_db("databaseName");
    5. echo "connected to database";
    6. mysql_query("INSERT INTO TableName (column1, column2) VALUES ('".$_REQUEST['user']."', '".$_REQUEST['comment']."')");
    7. echo "done. :D";
    8. mysql_close();
    9. ?>
    10.  
    Android code:
    Code (Text):
    1.  
    2. private void postComment()
    3.     {
    4.         String user = editName.getText().toString();
    5.         String comment = editComment.getText().toString();
    6.         OutputStream os = null;
    7.         ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
    8.         nameValuePair.add(new BasicNameValuePair("user", user));
    9.         nameValuePair.add(new BasicNameValuePair("comment", comment));
    10.         try
    11.         {
    12.             HttpClient client = new DefaultHttpClient();
    13.             HttpPost post = new HttpPost(ROTM_POST_URL);
    14.             post.setEntity(new UrlEncodedFormEntity(nameValuePair));
    15.             HttpResponse response = client.execute(post);
    16.            
    17.         }
    18.         catch(IOException e)
    19.         {
    20.             Toast.makeText(RoTM.this, "Unable to post comments", Toast.LENGTH_SHORT).show();
    21.         }
    22.         editName.setText("");
    23.         editComment.setText("");
    24.     }
    25.  
    Hope this helps. :)
     
  5. jamor

    jamor Android Expert

Loading...
Similar Threads - problems inserting online
  1. varamilc
    Replies:
    6
    Views:
    168
  2. varamilc
    Replies:
    7
    Views:
    433
  3. PDX_Dragon
    Replies:
    0
    Views:
    324
  4. callmefishmael
    Replies:
    4
    Views:
    179
  5. Somdatta
    Replies:
    7
    Views:
    227
  6. Wilma Swinson
    Replies:
    5
    Views:
    280
  7. LyleB
    Replies:
    2
    Views:
    239
  8. melani78
    Replies:
    12
    Views:
    742
  9. sbstorm
    Replies:
    0
    Views:
    439
  10. thricipio
    Replies:
    16
    Views:
    7,773

Share This Page

Loading...