problems inserting into online database


Last Updated:

  1. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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?
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Fixed it. Combination of a typo in the PHP file and a typo in the java code.
     
  3. rick99gtp

    rick99gtp Member

    Joined:
    Aug 6, 2010
    Messages:
    12
    Likes Received:
    0
    Can you post the correct code so I can understand what you did?
     
  4. jonbonazza

    jonbonazza Well-Known Member This Topic's Starter

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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 Well-Known Member

    Joined:
    Apr 13, 2010
    Messages:
    3,688
    Likes Received:
    761
    lmao
     

Share This Page

Loading...